prettyvoid
prettyvoid

Reputation: 3686

Activity lifecycle confusion

I'm new to Android development so please excuse my ignorance.

I have two activties, A + B. If go from A->B, A keeps retained and it does not get destroyed. When I go back from B->A (using the back button), activity A gets destroyed and created again then activity B gets destroyed.

I was looking for a way to retain activity A, so when I go back B->A, A is retained like it was. I was able to achieve this via setting activity A launchMode like so android:launchMode="singleTop"

Can anybody explain the initial behavior? or at least direct me to some material to read? Why is A destroyed when I press back in B?

What if I want to achieve the effect of android:launchMode="singleTop" programmatically, should I catch the "Back" call in B and create my own Intent with my own flags?

Is setting the launchMode to singleTop the correct way to achieve what I wanted?

Upvotes: 1

Views: 179

Answers (2)

David Wasser
David Wasser

Reputation: 95578

You are a bit confused. The situation you've described matches the behaviour of "UP" navigation. In your post you described going from B->A using the "BACK" button (hardware button on the device). But based on your description, you must be talking about the "UP" button (soft ">" icon in the action bar at the top of the screen). If you want to have the UP navigation use the existing instance of activity A (instead of creating a new one), then you need to set launchMode="singleTop" in the <activity> definition for activity A in the manifest.

See documentation for UP navigation for details about this. Here's a quote of the relevant bit:

To navigate up when the user presses the app icon, you can use the NavUtils class's static method, navigateUpFromSameTask(). When you call this method, it finishes the current activity and starts (or resumes) the appropriate parent activity. If the target parent activity is in the task's back stack, it is brought forward. The way it is brought forward depends on whether the parent activity is able to handle an onNewIntent() call:

  • If the parent activity has launch mode <singleTop>, or the up intent contains FLAG_ACTIVITY_CLEAR_TOP, the parent activity is brought to the top of the stack, and receives the intent through its onNewIntent() method.
  • If the parent activity has launch mode <standard>, and the up intent does not contain FLAG_ACTIVITY_CLEAR_TOP, the parent activity is popped off the stack, and a new instance of that activity is created on top of the stack to receive the intent.

Upvotes: 1

Piyush Kukadiya
Piyush Kukadiya

Reputation: 1890

When I go back from B->A (using the back button), activity A gets destroyed and created again.  

I think the way you are looking at concepts your above statement is wrong.

Activity A never get destroyed rather it goes into onStop() state of Activity lifecycle and when user press back button it resumes if you know the Activity Lifecycle

enter image description here

When the current activity starts another, the new activity is pushed on the top of the stack and takes focus. The previous activity remains in the stack, but is stopped. When an activity stops, the system retains the current state of its user interface. When the user presses the Back button, the current activity is popped from the top of the stack (the activity is destroyed) and the previous activity resumes (the previous state of its UI is restored).

EDITS :

activity A gets destroyed and created again  

This is possible in following cases:

  • If you don't explicitly shut down your first activity when starting the new one.

  • If you override the onBackPressed in your second activity to start the first one.
    Example:

    @Override public void onBackPressed() { startActivity(new Intent(this, FirstActivity.class)); }

Upvotes: 1

Related Questions