Reputation: 77984
I have three activities:
A,B,C
A is home screen.
Activities are launched as follow A->B->C
If I come to home screen using backbutton, I want to clear activity stack/previous activities history and it should exit the application.
Can anyone guide me how to achieve this?
Upvotes: 10
Views: 12215
Reputation: 1778
You can do following:
1. Set clearTaskOnLaunch = "true"
in AndroidManifest, in declaration of activity A
2. In activity C:
@Override
public void onBackPressed(){
moveTaskToBack(true);
}
So if user presses back - it comes back to homescreen.
If user launches application again - task stack clears and he comes to root activity (A)
Upvotes: 4
Reputation: 5146
I think compostus is true, but if not, from A you can launch B with startActivityForResult() and in onActivityResult() handle the received "message". Activity B will send a "CLOSE_ACTIVITY" message if back button has been pressed.
Upvotes: 0
Reputation: 64700
In Activity A try this:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0){
finish();
}
return super.onKeyDown(keyCode, event);
}
This should ensure that if you hit the back button the activity is finish()
ed. If this activity is at the bottom of the stack finish
should exit the activity.
Upvotes: 0
Reputation: 1228
Very simple: use intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
on an intent used to start the activity A.
Upvotes: 15