Question

I have more than 3 screens and every screen has logout button on top, when i click the logout button, it should go to login page, and if i click the back button on login page, it should not go to the application and exit from application.

here I have tried the code for logout button click:

Intent in = new Intent(activity, LoginActivity.class);
                    in.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                            | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    activity.startActivity(in);

Its working on more then 90 % of android mobile but its not working on few mobiles , Ex Samsung mini tab. and its not working on lower version of mobiles.

can any one guide me to over come this problem ?

Was it helpful?

Solution

Add your current activity into a list listContext as metioned below.

listContext.add(Activity.this);

Try this method to finish all your activity.

public static List<Context> listContext = new ArrayList<Context>();


    public static void finishActivities() {

        for (Context context : listContext) {
            if(context!=null)
             ((Activity) context).finish();
        }
            listContext.clear();
    }

OTHER TIPS

use this code on logout button activity:

    Intent setIntent = new Intent(this, LoginActivity.class);
    setIntent.addCategory(Intent.CATEGORY_HOME);
    setIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(setIntent);

and use this code on login screen :

    public void onBackPressed() {
    // TODO Auto-generated method stub
    Log.d("CDA", "onBackPressed Called");
    Intent setIntent = new Intent(Intent.ACTION_MAIN);
    setIntent.addCategory(Intent.CATEGORY_HOME);
    setIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(setIntent);
    return;
}

Disable back button on your login screen

@Override
public void onBackPressed() {
   //to nothing       }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top