Search code examples
androidback-stack

What is the order used to restore a stack of activities?


Android documentation provides a seemingly thorough description of Tasks and Back Stack, yet I can't find an answer to the following question: in which order do activities get restored (re-created) when their task (previously killed by the system) is about to become foreground again.

For example, if a task has activities A -> B -> C and get killed in background, then when a user switches back to it what would be the order of onCreate and onRestoreInstanceState event calls in all the activities in a row? Can we take it for granted that A is always completely restored before B, and B - completely restored before C?

The question arises from an error-log from Google Play which makes me suspect that main activity was not yet restored during the call to a child activity.


Solution

  • As a short (self-)answer based on some tests, I'm reporting that the order of activities restoration is from top to bottom of the stack, that is in the reverse order to the initial order in which the activities have been created. In the continuation of the example from the question, it would be C -> B -> A.

    Most important thing is that only a top activity is restored at once, and not the entire stack (which would be more intuitive and consistent, imho). Only after a user closes top activity, and the next activity is about to become new top one, that single activity is restored, and so on.

    For each activity a sequence of onCreate, onStart, onRestoreInstanceState is performed in adherence to the documentation.