42

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

SMR
  • 6,628
  • 2
  • 35
  • 56
AJ.
  • 4,526
  • 5
  • 29
  • 41
  • 2
    the `onDestroy` method is called after back press then i guess it should be flushed. – SMR Feb 25 '14 at 10:45
  • @SMR Can you provide some more details(any link). – AJ. Feb 25 '14 at 10:48
  • 1
    Every Activity comes to the stack for it's execution, activity removed from the stack from which you press back button.Means Activity B is removed from the stack. –  Feb 25 '14 at 10:48

6 Answers6

22

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()
PhilLab
  • 4,777
  • 1
  • 25
  • 77
Annada
  • 1,075
  • 1
  • 18
  • 21
16

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()
DAS
  • 686
  • 8
  • 13
8

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

SMR
  • 6,628
  • 2
  • 35
  • 56
  • The final call may be onDestroy, but other lifecycle methods will be called before that as @Amanda has answered. – Andrew S Aug 14 '23 at 18:56
2

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.

Rakesh
  • 1,133
  • 4
  • 13
  • 28
0

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
Harshal Benake
  • 2,391
  • 1
  • 23
  • 40
0

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.

steven0529
  • 1,513
  • 1
  • 18
  • 28