简体   繁体   中英

App closing when pressing go back button

I have an application that has the next diagram

http://i.stack.imgur.com/bSIFw.jpg

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.

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.

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM