Issue
I am a bit confused about how Android handles the activities. I am involved in a project with 3 main activities:
- A.- A splash / welcome screen
- B.- A list view about news
- C.- A news description that is shown when the user touches the new in the list view
If I am at C and OnDestroyed() is called, for example because an orientation change, is B also destroyed? when I come back to B, will I get the same list at the same point?
If B is destroyed when hidden because low resources, and onCreate() is called again, how do I recover the whole list? How can I recover the list without requesting it to the server again?
Solution
If I am at C and OnDestroyed() is called, for example because an orientation change, is B also destroyed?
No. When Activity B starts Activity C, Activity B is stopped and goes on to the back stack - as it is no longer visible, the concept of orientation is irrelevant at that point. In other words, Activity B is 'frozen' in its previous state. The only reason for destroying and recreating the visible Activity (in this case Activity C) is that the look / feel / behaviour of an Activity in different orientations may be subtly different and as such destroying / recreating the visible Activity is the cleanest way to ensure a fresh start.
when I come back to B, will I get the same list at the same point?
This depends. Imagine the following two scenarios...
Scenario 1
- In portrait, Activity B starts Activity C
- User rotates phone to landscape.
- Activity C is destroyed / recreated. All other Activities on the back stack remain unaffected
- User rotates phone back to portrait
- Activity C is again destroyed / recreated
- User presses BACK button and Activity C is permanently destroyed
- Activity B is restarted and goes through the restart / start / resume life-cycle stages and reappears complete with all data and state it previously had.
Scenario 2
- In portrait, Activity B starts Activity C
- User rotates phone to landscape.
- Activity C is destroyed / recreated. All other Activities on the back stack remain unaffected
- User presses BACK button and Activity C is permanently destroyed
- Activity B reappears briefly in portrait mode lying on its side because the phone is in landscape
- Due to change in orientation, Activity B is destroyed / recreated in landscape. This results in it going through creation, starting, resuming. Any previous data / state is obviously lost.
If B is destroyed when hidden because low resources...
Highly unlikely
...and onCreate() is called again, how do I recover the whole list? How can I recover the list without requesting it to the server again?
As I said in Scenario 2 whenever Activity B is destroyed / recreated, it will go through its whole life-cycle - if that includes downloading the list from the server then that's what will happen. There are ways around this, however, such as using onSaveInstanceState() or simply caching the list data somewhere locally and reloading from the cache.
Answered By - Squonk
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.