Reputation: 451
I have an application that has the next diagram
Once i arrive to activity B with the different previous path if I do home button and open the app again, correctly activity B is displayed BUT if I use "go back" button instead of home button, when I open the app again I get Activity A displayed instead of the last one that was Activity B.
Im finishing all my previous activity, one I arrive B, B is the only one active, all the previous (ACD) were close because If i hit "go back" on Activity B button i dont wanna go neither to AC or D just act send the app to the background.
Upvotes: 3
Views: 2779
Reputation: 5150
Just do like ..
In your activity B add the following code on back pressed button by overriding it..like:
Intent intent = new Intent(B.this, A.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);
And in your activity A
create a method something like:
private void exitApplication(){
if (getIntent().getBooleanExtra("EXIT", false)) {
finish();
}
}
and call it in onCreate()
Upvotes: 1
Reputation: 39406
When you press back, by default you finish the current activity.
If all your others activities are finished as well, the next time the application is opened, it can only open the default entry point (in your case A)
To achieve what you are trying to do, you have to keep your activities unfinished (therefore in the back stack), and override the behavior of the back button in B:
public void onBackPressed() {
moveTaskToBack(true);
}
This will put B in paused state, reveal the previous application (can be home, or can be an application from which you called your own application). Next time you start the application, it can be resumed directly in B.
Upvotes: 5