Question

I am little confused between the life cycle of two activities.

Suppose I have Activity A and Activity B.

B is called From A i.e A ----> B.

Now currently B is on the screen and I pressed back button. Here I want know:- is there any memory still available for B(Active) or B's memory is flushed(Inactive).

Was it helpful?

Solution

Suppose there is an activity A, from which you launch activity B. If, while in activity B, you hit the back button, you are popping activity B off the stack and B will not be in the activity stack any longer.

Whenever you push an activity to the stack, onCreate is called, and if you press back button, onDestroy is called, which means that the activity is flushed away.

stack

Please visit my blog for further information: http://upadhyayjiteshandroid.blogspot.in/2013/02/android-lifecycle.html

activity lifecycle please visit for more

http://developer.android.com/guide/components/tasks-and-back-stack.html

http://developer.android.com/training/basics/activity-lifecycle/starting.html

OTHER TIPS

The following activity call back methods are called, after pressing back button.

onPause()
onStop()
onDestroy()

The activity is destroyed.

And it recreates when launched again. These are the callback methods when it launches again.

onCreate()
onStart()
onResume()

The onDestroy method is called after back press. Then activity will be popped from the Activity back stack.

From docs:

If an activity is paused or stopped, the system can drop the activity from memory by either asking it to finish, or simply killing its process. When it is displayed again to the user, it must be completely restarted and restored to its previous state.

onDestroy() from docs:

The final call you receive before your activity is destroyed. This can happen either because the activity is finishing (someone called finish() on it, or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing() method.

enter image description here

I know the answer is been accepcted, still if this helps someone I am putting it.

When app is opening for the first time, by clicking the Icon

onCreate()
onStart()
onResume()

When home button is pressed

onPause()
onStop()

when app is again opened by clicking the app icon or launched from recent

onRestart()
onStart()
onResume()

when app is opened and then back button is pressed

onPause()
onStop()
onDestroy()

Activity B will be destroyed and will no longer remain in memory.

For more information please visit the official documentation for android and have a look at the activity life cycle figure.

Once you press the back key the activity's onDestroy() method will be called and the activity will be flushed out of the memory. You will then be required to restart the activity by calling the startActivity() method which will in turn call its onCreate() Method.

I would suggest to refer following link for activity lifecycle

http://stackoverflow.com/a/8516056/3110609

and following link for launch mode of activity.

www.intridea.com/blog/2011/6/16/android-understanding-activity-launchmode

After pressing the back button, Activity B will b destroyed. You see, Android Manages Activities like a Stack(an explanation of a stack). Everytime you start an activity, it pushes into the Activity Stack. So when Activity A calls Activity B, Activity B is now on top of Activity B, and when you press the back button, it also does a pop in the Activity Stack. So in concept, Activity B is gone. Pressing a Home Button is different from pressing back, it pauses the Activity, therefore it still eats a little of the phone's memory.

Here is a good explanation of how Android Manages Activities.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top