Each application installed on the device runs within its process.
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()
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.