The application starts and A activity appears (initial activity). Pressing a button in the activity A , we get into the activity B. In activity B , there is a button to return to activity A (I used the Intent function of the button). Press the button and get into activity A , that is, the initial activity. Then click Back, get into activity B. I would not want to get into activity B when I click Back, but quit the application. How can I do that?

What I've done?

Suppose we are in activity B , which is from A. Pressing the button in B , we got into activity A. The last function that worked for B was onStop() . In this function, I called onDestroy () to remove it. But the application displayed an error.

  • If “in activity B” to return to “activity A” is not used in Intent, but Back, then in “Activity A”, pressing Back will not return to “activity B” in any way. Well, purely from general considerations ... - Akina
  • one
    You do not need to delete activations, you need to work with a transition stack or with activation flags. Even if the activation is deleted, then when accessing the transition stack (by pressing the Back button), it will be loaded again. onDestroy() is a life cycle method that is called by the system when onDestroy() is destroyed, so that the programmer can perform any actions when this event occurs, he doesn’t delete anything and cause it (for bypassing the system) forcibly causing problems - pavlofff
  • one
    I think finish () and intent.addFlags (Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); it will help you, only in your case you will have 2 A activations, the one you started with, and the ones you created from B - PeDuCKA
  • one
    Where you prescribe intent for what you prescribe it? Use finish() instead of intent in Activate B and by pressing the BACK button in Activate A, the application will close. - Tommy

1 answer 1

When you press the button in B and hit A, it is not there. And with which you hit B. In your case, a new instance of activation is launched and the activation stack looks like A -> B -> A. Accordingly, when pressed back, it spins up in back side. In Activit B, you just need to call finish() , and not launch a new activation.

I also advise you to read about the activation stack and tasks in the official documentation