All activities in my application require a user to be logged-in to view. Users can log out from almost any activity. This is a requirement of the application. At any point if the user logs-out, I want to send the user to the Login Activity
. At this point I want this activity to be at the bottom of the history stack so that pressing the "back" button returns the user to Android's home screen.
I've seen this question asked a few different places, all answered with similar answers (that I outline here), but I want to pose it here to collect feedback.
I've tried opening the Login activity by setting its Intent
flags to FLAG_ACTIVITY_CLEAR_TOP
which seems to do as is outlined in the documentation, but does not achieve my goal of placing the Login activity at the bottom of the history stack, and preventing the user from navigating back to previously-seen logged-in activities. I also tried using android:launchMode="singleTop"
for the Login activity in the manifest, but this does not accomplish my goal either (and seems to have no effect anyway).
I believe I need to either clear the history stack, or finish all previously- opened activities.
One option is to have each activity's onCreate
check logged-in status, and finish()
if not logged-in. I do not like this option, as the back button will still be available for use, navigating back as activities close themselves.
The next option is to maintain a LinkedList
of references to all open activities that is statically accessible from everywhere (perhaps using weak references). On logout I will access this list and iterate over all previously-opened activities, invoking finish()
on each one. I'll probably begin implementing this method soon.
I'd rather use some Intent
flag trickery to accomplish this, however. I'd be beyond happy to find that I can fulfill my application's requirements without having to use either of the two methods that I've outlined above.
Is there a way to accomplish this by using Intent
or manifest settings, or is my second option, maintaining a LinkedList
of opened activities the best option? Or is there another option that I'm completely overlooking?
I can suggest you another approach IMHO more robust. Basically you need to broadcast a logout message to all your Activities needing to stay under a logged-in status. So you can use the sendBroadcast
and install a BroadcastReceiver
in all your Actvities. Something like this:
/** on your logout method:**/
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("com.package.ACTION_LOGOUT");
sendBroadcast(broadcastIntent);
The receiver (secured Activity):
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/**snip **/
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.package.ACTION_LOGOUT");
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("onReceive","Logout in progress");
//At this point you should start the login activity and finish this one
finish();
}
}, intentFilter);
//** snip **//
}
It seems a rite of passage that a new Android programmer spends a day researching this issue and reading all of these StackOverflow threads. I am now newly initiated and I leave here trace of my humble experience to help a future pilgrim.
First, there is no obvious or immediate way to do this per my research (as of September 2012).
You'd think you could simple startActivity(new Intent(this, LoginActivity.class), CLEAR_STACK)
but no.
You CAN do startActivity(new Intent(this, LoginActivity.class))
with FLAG_ACTIVITY_CLEAR_TOP
- and this will cause the framework to search down the stack, find your earlier original instance of LoginActivity, recreate it and clear the rest of the (upwards) stack. And since Login is presumably at the bottom of the stack, you now have an empty stack and the Back button just exits the application.
BUT - this only works if you previously left that original instance of LoginActivity alive at the base of your stack. If, like many programmers, you chose to finish()
that LoginActivity
once the user has successfully logged in, then it's no longer on the base of the stack and the FLAG_ACTIVITY_CLEAR_TOP
semantics do not apply ... you end up creating a new LoginActivity
on top of the existing stack. Which is almost certainly NOT what you want (weird behavior where the user can 'back' their way out of login into a previous screen).
So if you have previously finish()
'd the LoginActivity
, you need to pursue some mechanism for clearing your stack and then starting a new LoginActivity
. It seems like the answer by @doreamon
in this thread is the best solution (at least to my humble eye):
https://stackoverflow.com/a/9580057/614880
I strongly suspect that the tricky implications of whether you leave LoginActivity alive are causing a lot of this confusion.
Good Luck.
UPDATE
the super finishAffinity()
method will help to reduce the code but achieve the same. It will finish the current activity as well as all activities in the stack, use getActivity().finishAffinity()
if you are in a fragment.
finishAffinity();
startActivity(new Intent(mActivity, LoginActivity.class));
ORIGINAL ANSWER
Assume that LoginActivity --> HomeActivity --> ... --> SettingsActivity call signOut():
void signOut() {
Intent intent = new Intent(this, HomeActivity.class);
intent.putExtra("finish", true);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // To clean up all activities
startActivity(intent);
finish();
}
HomeActivity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
boolean finish = getIntent().getBooleanExtra("finish", false);
if (finish) {
startActivity(new Intent(mContext, LoginActivity.class));
finish();
return;
}
initializeView();
}
This works for me, hope that it is helpful for you too. :)
If you are using API 11 or higher you can try this: FLAG_ACTIVITY_CLEAR_TASK
--it seems to be addressing exactly the issue you're having. Obviously the pre-API 11 crowd would have to use some combination of having all activities check an extra, as @doreamon suggests, or some other trickery.
(Also note: to use this you have to pass in FLAG_ACTIVITY_NEW_TASK
)
Intent intent = new Intent(this, LoginActivity.class);
intent.putExtra("finish", true); // if you are checking for this in your other Activities
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_CLEAR_TASK |
Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
finish();
will do the job to prevent going back after logout. What's the need of setting flags?
I spent a few hours on this too ... and agree that FLAG_ACTIVITY_CLEAR_TOP sounds like what you'd want: clear the entire stack, except for the activity being launched, so the Back button exits the application. Yet as Mike Repass mentioned, FLAG_ACTIVITY_CLEAR_TOP only works when the activity you're launching is already in the stack; when the activity's not there, the flag doesn't do anything.
What to do? Put the activity being launching in the stack with FLAG_ACTIVITY_NEW_TASK, which makes that activity the start of a new task on the history stack. Then add the FLAG_ACTIVITY_CLEAR_TOP flag.
Now, when FLAG_ACTIVITY_CLEAR_TOP goes to find the new activity in the stack, it'll be there and be pulled up before everything else is cleared.
Here's my logout function; the View parameter is the button to which the function's attached.
public void onLogoutClick(final View view) {
Intent i = new Intent(this, Splash.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);
finish();
}
FLAG_ACTIVITY_CLEAR_TASK
wasn't added yet
Lots of answers. May be this one will also help-
Intent intent = new Intent(activity, SignInActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.startActivity(intent);
this.finish();
Kotlin version-
Intent(this, SignInActivity::class.java).apply {
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
}.also { startActivity(it) }
finish()
Use this it should be helpful to you. Slightly modified xbakesx answer.
Intent intent = new Intent(this, LoginActivity.class);
if(Build.VERSION.SDK_INT >= 11) {
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);
} else {
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
}
startActivity(intent);
Accepted solution is not correct, it has problems as using a broadcast receiver is not a good idea for this problem. If your activity has already called onDestroy() method, you will not get receiver. Best solution is having a boolean value on your shared preferences, and checking it in your activty's onCreate() method. If it should not be called when user is not logged in, then finish activity. Here is sample code for that. So simple and works for every condition.
protected void onResume() {
super.onResume();
if (isAuthRequired()) {
checkAuthStatus();
}
}
private void checkAuthStatus() {
//check your shared pref value for login in this method
if (checkIfSharedPrefLoginValueIsTrue()) {
finish();
}
}
boolean isAuthRequired() {
return true;
}
onResume()
methode. Because when you press the back button, the activity is more likely to get resumed instead of created.
Sometime finish()
not working
I have solved that issue with
finishAffinity()
Here is the solution I came up with in my app.
In my LoginActivity, after successfully processing a login, I start the next one differently depending on the API level.
Intent i = new Intent(this, MainActivity.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
startActivity(i);
finish();
} else {
startActivityForResult(i, REQUEST_LOGIN_GINGERBREAD);
}
Then in my LoginActivity's onActivityForResult method:
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB &&
requestCode == REQUEST_LOGIN_GINGERBREAD &&
resultCode == Activity.RESULT_CANCELED) {
moveTaskToBack(true);
}
Finally, after processing a logout in any other Activity:
Intent i = new Intent(this, LoginActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);
When on Gingerbread, makes it so if I press the back button from MainActivity, the LoginActivity is immediately hidden. On Honeycomb and later, I just finish the LoginActivity after processing a login and it is properly recreated after processing a logout.
I'd suggest a different approach to this question. Maybe it's not the most efficient one, but I think it's the easiest to apply and requires very little code. Writing the next code in your first activity (log in activity, in my case) won't let the user go back to previously launched activities after logging out.
@Override
public void onBackPressed() {
// disable going back to the MainActivity
moveTaskToBack(true);
}
I'm assuming that LoginActivity is finished just after the user logs in, so that he can't go back to it later by pressing the back button. Instead, the user must press a log out button inside the app in order to log out properly. What this log out button would implement is a simple intent as follows:
Intent intent = new Intent(this, LoginActivity.class);
startActivity(intent);
finish();
All suggestions are welcome.
The selected answer is clever and tricky. Here's how I did it:
LoginActivity is the root activity of the task, set android:noHistory="true" to it in Manifest.xml; Say you want to logout from SettingsActivity, you can do it as below:
Intent i = new Intent(SettingsActivity.this, LoginActivity.class);
i.addFlags(IntentCompat.FLAG_ACTIVITY_CLEAR_TASK
| Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
This worked for me:
// After logout redirect user to Loing Activity
Intent i = new Intent(_context, MainActivity.class);
// Closing all the Activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
// Add new Flag to start new Activity
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Staring Login Activity
_context.startActivity(i);
Start you activity with StartActivityForResult and while you logout set your result and according to you result finish your activity
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivityForResult(intent, BACK_SCREEN);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case BACK_SCREEN:
if (resultCode == REFRESH) {
setResult(REFRESH);
finish();
}
break;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
AlertDialog alertDialog = builder.create();
alertDialog
.setTitle((String) getResources().getText(R.string.home));
alertDialog.setMessage((String) getResources().getText(
R.string.gotoHome));
alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
setResult(REFRESH);
finish();
}
});
alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
}
});
alertDialog.show();
return true;
} else
return super.onKeyDown(keyCode, event);
}
The solution @doreamon provided works fine for all the cases except one:
If After login, Killing Login screen user navigated direct to a middle screen. e.g. In a flow of A->B->C, navigate like : Login -> B -> C -> Press shortcut to home. Using FLAG_ACTIVITY_CLEAR_TOP clears only C activity, As the Home(A) is not on stack history. Pressing Back on A screen will lead us back to B.
To tackle this problem, We can keep an activity stack(Arraylist) and when home is pressed, we have to kill all the activities in this stack.
It is possible by managing a flag in SharedPreferences or in Application Activity.
On starting of app (on Splash Screen) set the flag = false; On Logout Click event just set the flag true and in OnResume() of every activity, check if flag is true then call finish().
It works like a charm :)
on click of Logout you may call this
private void GoToPreviousActivity() {
setResult(REQUEST_CODE_LOGOUT);
this.finish();
}
onActivityResult() of previous Activity call this above code again until you finished the all activities.
One option is to have each activity's onCreate check logged-in status, and finish() if not logged-in. I do not like this option, as the back button will still be available for use, navigating back as activities close themselves.
What you want to do is call logout() and finish() on your onStop() or onPause() methods. This will force Android to call onCreate() when the activity is brought back on since it won't have it in its activity's stack any longer. Then do as you say, in onCreate() check logged in status and forward to login screen if not logged in.
Another thing you could do is check logged in status in onResume(), and if not logged in, finish() and launch login activity.
Success story sharing