How can I find out that activity will be killed by the OS?

I know that I can use isFinishing () in onPause () to find out if the action will be killed by finish ().

Then, how can I find out that the activity will be temporarily turned off by the OS due to low memory?

Thanks.

+4
source share
2 answers

In docs , onDestroy should be called right before the action is destroyed, regardless of the reason. If a finish was requested, isFinishing will return true. Therefore, if this is a lie, you can assume that the system should have finished.

However, as the docs say,

Note: do not count on this method called data storage location!

In general, you cannot guarantee that your activity will be killed beautifully. Things like task killers get involved in the life cycle.

Use onPause or onSaveInstanceState to save correctly.

+2
source

You can not.

Perhaps your activity may disappear without the rest of your application, in which case onDestroy will be called. However, it is also possible that your application will be killed immediately, it's like kill -9 on unix. Currently, your application cannot run any code; it was killed instantly and without warning.

To deal with this correctly, you want to create your own application to save all the important information on disk to onPause and be prepared to receive it later in onCreate if necessary.

+2
source

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


All Articles