miguel
miguel

Reputation: 16580

Application startup lifecycle

I'm unable to find good documentation on an Application's long term lifecycle.

If I have the app running, hit the home button, and then click on the app's launcher icon the Application's onCreate is not called and the front Activity's onResume is called. But presumably if I wait long enough at some point clicking the launcher icon will call Application onCreate and the MAIN LAUNCHER activity is started. When does this happen?

When I go through the apps in the Recents list if I click on a really old app that I haven't used in a month the app does not return to its previous state, instead the app goes through its startup. So it seems at some point apps move from a saved state to no saved state. When does this happen?

If the app gets killed due to memory pressure presumably the savedInstanceState bundle is saved and Activity onResume is called, but does Application's onCreate get called at that point? How long is the bundle saved for?

Upvotes: 2

Views: 787

Answers (2)

dawid gdanski
dawid gdanski

Reputation: 2452

Each application installed on the device runs within its process.

  1. If you enter the application for the first time, the following sequence is called:

    • Application.onCreate()
    • YourFirstActivity.onCreate() (provided that the YourFirstActivity is declared in AndroidManifest.xml)
    • YourFirstAcitvity.onStart()
    • YourFirstActivity.onResume()
  2. If you click the Home button, the application goes to background and the following callback methods are called:

    • YourCurrentActivity.onPause()
    • YourCurrentActivity.onSaveInstanceState() - this invocation is not specified exactly but you can assume that by the onStop() method is called in most of the cases the onSaveInstanceState() should be called.
    • YourCurrentActivity.onStop();

While the application is in background it is not specified how long it will be there.

It is up to the System to maintain it in background.

Many applications, while your is in background, perform periodic syncs, run scheduled services or simply run in foreground when launched and for that purpose the Android OS must somehow find memory to execute all this logic. Thus, if there is a shortage in the required memory then the OS kills processes (e. g. your application).

So if you hide your application to the background and click on either the application launcher icon or return to it from Recent apps list immediately, the following sequence of callback methods are executed (assuming that you hid the app while being in YourCurrentActivity):

  • YourCurrentActivity.onRestart();
  • YourCurrentActivity.onStart();
  • YourCurrentActivity.onRestoreInstanceState();
  • YourCurrentActivity.onResume();

However, if you do not reenter the hidden application for a longer period then it is high probability that the OS has already killed your application in favour of meeting other applications requirements.

If this happens the following sequence of callback methods are called:

  • Application.onCreate()
  • YourCurrentActivity.onCreate();
  • YourCurrentActivity.onStart();
  • YourCurrentActivity.onResume();

Notice please that it is still YourCurrentActivity that you left when hiding the application to the background.

The following sequence is executed because System creates new process for your application as @CommonsWare points.

How to prevent the app from being killed by the System? (Android - implementing startForeground for a service?)

Hope this helps somehow.

Upvotes: 3

CommonsWare
CommonsWare

Reputation: 1006674

But presumably if I wait a long enough at some point clicking the launcher icon will call Application onCreate and the MAIN LAUNCHER activity is started. When does this happen?

These are separate issues.

The Application singleton is created shortly after the process is forked, as part of spinning up a new process for you. This will be triggered when something needs your app to exist (e.g., user taps on the launcher icon) and your process does not already exist. Processes do not live forever. How long any given process remains in memory depends on a variety of environmental factors.

Whether the launcher activity resumes an existing task, or whether your task is reset, depends upon whether the task exists (i.e., did the user swipe the task off the recent-tasks list) and how long it has been since the user left the task. A task is considered "alive" for about 30 minutes, though there are manifest entries that you can use to try to adjust this a bit.

So it seems at some point apps move from a saved state to no saved state. When does this happen?

About 30 minutes (see previous paragraph).

If the app get's killed due to memory pressure

I presume that by "app get's killed" you mean "the app's process is terminated".

presumably the savedInstanceState bundle is saved and Activity onResume is called, but does Application's onCreate get called at that point?

Yes, because a new process needs to be created for you.

How long is the bundle saved for?

About 30 minutes (see above).

Upvotes: 4

Related Questions