Application Application Lifecycle Documentation

I am trying to find the official documentation of the Android Application class lifecycle. Apparently, for what I found in StackOverflow here and here, the Application class can be killed if the system needs memory. Even this tutorial says so.

But little annoys me a bit:

  • I cannot find the official documentation telling me that yes, the Application class can be killed on low memory.
  • I cannot find any official diagram representing the Application life cycle.
  • I cannot find the correct answer to use if the Application class is killed other than onLowMemory() . Does this mean that I should use this method to save my data?
  • If the Application class is killed at low memory pressure and the application reappears in the foreground, how can I find out in my onCreate() that the application was recreated after the system killed? In Activity I would test savedInstanceState , but as far as I know, there is nothing like that in the Application class.

Thank you for your enlightenment.

+5
source share
2 answers

I cannot find the official documentation telling me that yes, the application class can be killed in low memory.

Below are links to where it was indicated:

I cannot find any official diagram representing the application life cycle.

This is a reasonable remark ... Although the following opinion is based on opinions, I believe that such a diagram contradicts the philosophy of "multitasking" for Android, as described in the last link:

β€œThe key to how Android processes applications in this way is that processes do not close cleanly. When a user leaves the application, his process is supported in the background, which allows him to continue to work (for example, loading web pages of a page) if this is necessary, and immediately go to the forefront if the user returns to it.If the device never has enough memory, then Android will support all these processes, really leaving all applications β€œrunning” all the time. ”

I cannot find the proper callback to use if the application class is killed with the exception of onLowMemory() . Does this mean that I should use this method to save my data?

Regarding onLowMemory() , whose description is pretty simple, are we talking about the background process or the foreground of the UI? ...

If none of the Activities applications is in the foreground and the operating system is inactive in memory, this may lead to the removal of the application, so that none of the Application callbacks or the application component ( Activity , Service ) will be called, However ( since you are dealing with Activities ), I recommend storing all persistent data in accordance with the documentation in onPause () .

If the application class is killed at low memory pressure and the application reappears in the foreground, how can I find in onCreate() that the application was restored after a system crash?

You cannot recognize it in Application onCreate() .

+4
source

As far as I know, you cannot handle the event killed by the application. Here is a quote from the onTerminate method application:

This method is intended for use in emulated process environments. This will never be called on an Android production device, where processes are deleted simply by killing them; no user code (including this callback) is executed at the same time.

The general idea is that you don't care if the application was killed or not. If so, the OS will restart the application the next time it is necessary, otherwise it will be resumed (and for this you will use the Activity / Fragment life cycle events).

What data do you need to store - is it possible to save it earlier (when it is received from a web service, etc.) instead of waiting for the last moment?

+1
source

Source: https://habr.com/ru/post/1243366/


All Articles