简体   繁体   中英

Clear the Activity stack when I go to an activity

I am working on an application with several Activities, problem is that I want the user to be able to log out by pressing a button.

Suppose we have 4 Activity named A,B,C,D. Navigation of the activity like B->C->D.

On Activity D user have options for logout. When user click on the Logout button the he go to Activity A which is not called in the navigation. Now, user click on the back button then he got to previous activity like Activity D.

I already tried to launch the Activity with the two following flags:

Intent intent = new Intent(this, LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);        
startActivity(intent);

Can anyone help?

Okay, I made myself a small project to somewhat simulate what you have. I'm going to leave out the imports in the code below. Also, read all the code and comments, I didn't write them to have you skip over them. Let's call the classes splash, login, page1, page2, and page3. In your splash class, before the onCreate() method, let's put a few things.

package com.test.jeets;

public class splash extends Activity {

    //Add all of these right here!
    public static boolean loggedin = false;
    public static int sdk = new Integer(Build.VERSION.SDK).intValue();

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
        //This is your splash screen, all the code goes here!
    }
}

Now that's your splash screen, Done, For the login screen. we'll want to make sure it can't go back to the splash screen, and instead exit out of the app when back is pressed. That's easy! Let's just override the back button and use moveTaskToBack(true) .

package com.test.jeets;

public class login extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        //This is the login page stuff. Nothing special needs done here.
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)  {
        if (splash.sdk < 5 && keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            onBackPressed();
        }

        return super.onKeyDown(keyCode, event);
    }
    //This will make the back button exit the app to the home screen.
    @Override
    public void onBackPressed() {
        moveTaskToBack(true);
        return;
    }
    //If the user leaves the app from the login screen while not logged in, they are returned to the splash screen.
    @Override
    public void onRestart() {
        super.onRestart();
        if(splash.loggedin == false){
        Intent i = new Intent(login.this, splash.class);
        startActivity(i);}
    }
}

Now, that's done! Let's go to your first page (the page you go to after you're logged in) and add a line in the onCreate() method

splash.loggedin = true;

You might want to consider having a user back out of this page log them out as well. Not necessary, but certainly do-able.

The last thing you'll need to do is set that variable to false when a user logs out with the logout button. So in the code for the button, before the call to start the login activity again, just add splash.loggedin = false; . Now when the button is clicked, it will log the user out, set the variable to false, and take them to the login screen. Like I said, I tested this with a simple layout and real basic switch between activities and it worked fine for me.

Please try this in your Activity D logout event.

 Button button=(Button)findViewById(R.id.btnLogout);
        button.setOnClickListener(new View.OnClickListener() {

@Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent=new Intent(context,ActivityA.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); context.startActivity(intent); } });</PRE>

Try

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);

You remove FLAG_ACTIVITY_NEW_TASK by the second call.

Finally this code is working for me.......

When I press back button on Login activity then I go to Home screen of android device.

Intent intent= new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_HOME); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM