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.
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).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()methodYou 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.