Huzo
Huzo

Reputation: 1692

How do we retrieve saved state if the Activity is killed by OS?

So, as far as I have understood, once the App is not destroyed and is in the background; if the OS requires more memory the OS kills the app but saves the state (onSaveInstanceState). And when we re-open the app, it would seem like we are facing our previous activity but it actually has been destroyed and created again. If my interpretation is correct, how does the App retrieve the saved state? Does it store it in memory? For how long are we able to retrieve the saved state?

Upvotes: 1

Views: 37

Answers (2)

Anatolii
Anatolii

Reputation: 14660

If my interpretation is correct, how does the App retrieve the saved state?

From the Android documentation

If the system destroys the activity due to system constraints (such as a configuration change or memory pressure), then although the actual Activity instance is gone, the system remembers that it existed. If the user attempts to navigate back to the activity, the system creates a new instance of that activity using a set of saved data that describes the state of the activity when it was destroyed.

Regarding your second question - it's the implementation detail how the OS does it and it's actually shouldn't worry us :). What's important is that it should do it reliably.

The system will keep the saved state as long as the user doesn't press Back or finish() of your Activity is not called.

Upvotes: 2

Gabe Sechan
Gabe Sechan

Reputation: 93614

The OS stores it. It calls onSaveInstanceState to let you generate a Bundle it stores, and will call onRestoreInstanceState to let you restore yourself from the state. How the OS stores it doesn't matter- maybe it saves it in RAM, maybe it serializes it to disk. What is assured is that if you go back to that activity you will be passed a Bundle object with the information you previously filled in. You do not need to retrieve the state- if it exists it will be passed to you.

Upvotes: 1

Related Questions