Life cycle of Android Activity after pressing Back button
Asked Answered
D

6

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

Degression answered 25/2, 2014 at 10:43 Comment(3)
the onDestroy method is called after back press then i guess it should be flushed.Fission
@Fission Can you provide some more details(any link).Degression
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.Wearable
W
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()
Whang answered 14/4, 2016 at 10:57 Comment(0)
H
17

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()
Herriot answered 4/12, 2018 at 11:4 Comment(1)
Some of this seems to be deice-dependent: Setup: new project, log lifecycle events. Device1: calls onStop & onDestroy when navigating back. Device2: calls only onStop. However, both devices trigger onStop when pressing the home button. Frustrating!Twinkle
F
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

Fission answered 25/2, 2014 at 10:58 Comment(1)
The final call may be onDestroy, but other lifecycle methods will be called before that as @Amanda has answered.Thorianite
H
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.

Herringbone answered 25/2, 2014 at 10:46 Comment(2)
Please edit your answer with all the info if you could, thanks.Cloudburst
The lifecycle figure in Android's documentation has never explicitly shown what the effect of the back button does.Thorianite
T
0

I would suggest to refer following link for activity lifecycle

https://mcmap.net/q/80309/-android-activity-life-cycle-what-are-all-these-methods-for

and following link for launch mode of activity.

www.intridea.com/blog/2011/6/16/android-understanding-activity-launchmode
Telamon answered 25/2, 2014 at 11:1 Comment(0)
M
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.

Multifold answered 25/2, 2014 at 11:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.