Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android application lifecycle clarification

Can anybody confirm the following regarding the android application lifecycle?

1) When application is in foreground the memory will contain instance of the Application object, instances of all activities (not killed) and all the object references that are referenced from one of the root's (haven't been garbage collected)

2) When application goes to background, at some point the Android Framework can: a) Kill the whole process given to the purpose of the application which will essentialy erase all the objects from the memory b) Kill ONLY (so essentialy no other object reference will be deleted) the activities (by finishing them and in essence any fragments as well) saving their states and creating the Activities Stack and leaving anything else (Application object, any other static objects, references that are reachable from any of the roots).

I'm mostly interested in 2b, but I would appriciate confirmation on all of these points as I'm trying to grasp mentaly the whole concept from start to finish.

like image 989
Lucas Avatar asked Nov 17 '15 07:11

Lucas


People also ask

What is the life cycle of an Android application?

An Android activity goes through six major lifecycle stages or callbacks. These are: onCreate() , onStart() , onResume() , onPause() , onStop() , and onDestroy() . The system invokes each of these callbacks as an activity enters a new state.

What is the use of an onStop () function in Android?

The onStop() function is called when the application enters the stopped state. In this state, the activity is no longer visible to the user. This may happen for the following reasons, the user performing some action that invoked another activity to come to the foreground or the activity finished.

What is the life cycle of an app?

Typically, application lifecycle management characterizes itself in six stages: development, introduction, growth, maturity, saturation, and decline. The duration of these stages fully depends on the product or application itself, the complexity, and the fit in the market.


2 Answers

If you are looking for official confirmation then better ask Google only :).

but i feel after reading this you will get a better understanding of these concept.

Android memory management

android process lifecycle

Answer for 1st question: yes confirm using DDMS.

answer for 2a question: yes OS can kill process any point of time when needed memory for other process which will result into killing all object related to process.

answer for 2b questiong:

From official documentation

Process Lifecycle 3. A background activity (an activity that is not visible to the user and has been paused) is no longer critical, so the system may safely kill its process to reclaim memory for other foreground or visible processes. If its process needs to be killed, when the user navigates back to the activity (making it visible on the screen again), its onCreate(Bundle) method will be called with the savedInstanceState it had previously supplied in onSaveInstanceState(Bundle) so that it can restart itself in the same state as the user last left it.

like image 199
CodingRat Avatar answered Nov 15 '22 21:11

CodingRat


Yes you are mostly correct about 2b).

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.

However there are instances where onSaveInstantSate isn't called:

Note that it is important to save persistent data in onPause() instead of onSaveInstanceState(Bundle) because the latter is not part of the lifecycle callbacks, so will not be called in every situation as described in its documentation.

Android Docs Source

You can request android to always destroy activities on background by enabling the following developer option. If you are debugging your app you should be able to step through the life cycle methods and see what is happening.

Settings -> Developer options -> Apps -> Don't keep activities

like image 25
Jim Baca Avatar answered Nov 15 '22 19:11

Jim Baca