Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

details of Android application components destruction and recreation

Tags:

Could someone push me in the direction of some concrete, trustworthy (and preferably concise) information on the following:

  1. The order in which components are destroyed and (where applicable)recreated by the system (Fragment, Activity, Activity's Threads/AsyncTasks/Timers, static data(when are classes unloaded?), Threads/AsyncTasks/Timers in other classes, host TabActivity, ActivityGroup, bound local Services, Application, process) both when the app is in the background, and in the foreground.
    At which points the destruction can stop (what states can be encountered upon return to the app - like "everything including the Application object destroyed, process alive"?

  2. Is it possible (without modifying Android) to programmatically cause the same kind of destruction ourselves, so that it is indistinguishable from when the system does it, or is a separate mechanism needed for when we ourselves choose to free memory (triggered by onLowMemory)?

  3. Reliable reproduction steps of all scenarios from 1) (would junit.framework do? I haven't investigated that) ?

  4. "If the user leaves a task for a long time, the system clears the task of all activities except the root activity. When the user returns to the task again, only the root activity is restored": is this aside from the process/component lifecycle/destruction, or tied to it?

I have read various sources give pieces of information, often incomplete and misleading, sometimes incorrect.
I admit, I have skimmed over some parts of the documentation, so i may have missed or misunderstood something.

[EDIT] To avoid misunderstandings: What I am asking about is Android destroying components to free memory, bypassing Activity.onDestroy.
When I put the app in the background and return later, the one of these sequences will occur:

  • onPause, onStop, onRestart, onStart, onResume
  • onPause, onStop, Application.onCreate, onCreate(notNull), onStart, onResume

[EDIT2] Bounty started. Need reliable info on: Activities, Fragments, Application, bound (potentially remote)Services, process.
Partial/full destruction scenarios. See 1st point.

like image 496
kaay Avatar asked Nov 08 '11 11:11

kaay


People also ask

What are the main components of an Android application and explain their role?

Android applications are broken down into four main components: activities, services, content providers, and broadcast receivers. Approaching Android from these four components gives the developer the competitive edge to be a trendsetter in mobile application development.

What is application component?

Android applications are developed using JAVA, Kotlin, and C++. Application components are very essential for building Applications. They work as an entry point for users or system to enter your application. There are four different types of components. Each component has its own purpose and distinct life cycle.


1 Answers

Credit for this goes to hackbod for writing it here(read the whole answer), and to CommonsWare for linking in a comment.

In short: All the docs, rewritten many times, continue to lie to us. They're not misleading, they just give us untrue information. Unless you're using Fragments (dunno if support v4 counts, too), Android frees memory by killing the whole process, or does nothing.

Of course, this does not address everything:

  • Point 4 of the question
  • Why I frequently saw onCreate(notNull) on pressing Back on an Activity stack with Activities handling all config changes (Android 2.3.7)
  • How this relates to the widely accepted belief that onPause is the last call you are certain to get, and onStop may never get called (how, then, can the app go to the background, to be killed?)

We're getting somewhere, though.

like image 176
kaay Avatar answered Oct 22 '22 02:10

kaay