Reputation: 24265
I have an app with two activities. MainActivity
(which is the launcher activity) and a LoginActivity
. As soon as the user launches the app, MainActivity
is skipped over and LoginActivity
is displayed. However, if you press the back button, you can still navigate to MainActivity
. I'm using the following code to stop that from happening, however it is not working.
In MainActivity
class:
Intent intent = new Intent(this, LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
Any ideas?
Upvotes: 0
Views: 69
Reputation: 2877
you have two options First: if you want back to work but MainActivity should not be there on Back then finish() MainActivity.
like
Intent intent = new Intent(this, LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();//this line will finish MainActivity, then there will no instance of MainActivity in backstack.
Second: If you to stop back button to work , you can override OnBackPressed() metjod and remove super from that...then back button will not work (of device).
Upvotes: 0
Reputation: 5495
Intent intent = new Intent(this, LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
If you set twice, the last one will be set.
Upvotes: 1
Reputation: 6426
just use finish();
after startActivity(intent);
... finish(); will kill the activity as soon as you navigate from it.
Upvotes: 1
Reputation: 9103
just add finish();
in the last and change flags to CLEAR_TOP
rather that CLEAR_TASK
:
Intent intent = new Intent(this, LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
Upvotes: 0