Reputation: 5542
I am making an app where the user can logout by clicking the logout button. Before logging out the user is in the Activty A, then he clicks on the ogout button in the navigation drawer and this takes him to the Activity B( Login screen activity). Now here if I click the back button the Activity A reappears even though I am clearing the activity stack by the following code,
Intent intent = new Intent(curr_context, Activity_B.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
finish();
Also in the Activity B (login activity) I have the following code,
public void onBackPressed() {
if (backPressedToExitOnce) {
super.onBackPressed();
} else {
this.backPressedToExitOnce = true;
Toast.makeText(curr_context, "Press again to exit", Toast.LENGTH_LONG).show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
backPressedToExitOnce = false;
}
}, 2000);
}
}
I am really clueless as to where am I wrong. Any help would be appreciated. Thanks in advance !!
Upvotes: 0
Views: 69
Reputation: 21
you can add this in the activity A:
// 2.0 and above
@Override
public void onBackPressed() {
moveTaskToBack(true);
}
// Before 2.0
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
moveTaskToBack(true);
return true;
}
return super.onKeyDown(keyCode, event);
}
with this way, when you click back button it will take you to the launcher instead of the activity B
Upvotes: 1
Reputation: 2257
In Activty_A
call finish();
after launching Activity_B
The flags you set in the intent create a new task for B activity, but don't close A for you.
Upvotes: 1
Reputation: 11
When you started Activity A from Activity B did you call "finish()" after the StartActivity ?
Example in ActivityB:
Intent intentActivityA= new Intent(getApplicationContext(),ActivityA.class);
startActivity(intentActivityA);
finish();
This will terminate ActivityB just after starting Activity A, when you use the back button from ActivityA it should automatically terminate the application as ActivityB is already finished.
Upvotes: 1
Reputation:
just use the
finish();
i think this will do the trick i had the same problem and tell me if it works
Upvotes: 1