I have a single activity app that uses a PagerAdapter. In the OnCreate event I trigger the inflation of all 7 of the pages in the PagerAdapter. The various pages have standard widgets, one page has a google Map. It all works well when testing in the AVD.
But what if my app is paused or stopped and then restarted or resumed? Does the inflation of all my pages in the pageradapter get deleted? Do I have to reinflate all the pages again?
More generally…
I’ve read a lot of articles about what happens with my app gets stopped or paused and what I should do upon restart or resume but I haven’t seen a precise accounting of what needs to be rebuilt and what does not need to be rebuilt. I read that “this is a great place to refresh the UI” but I’m not sure exactly what that means.
Begging questions might be…
is my internal state restored i.e. values of my variables, sqlLite db, file contents.
are the values in my widgets restored i.e. characters in a textbox.
How can I prevent corruption if the pause or stop can happen in the middle of a for loop or a code block?
Thanks,
Gary
If by app you mean
Activity, when it is paused this just means it is not the focusedActivityright now. All the state is still there. This basically means that you cannot accept any foreground events untilonResume(). If the OS decides to call,onSaveInstanceState(), you can can actually store things like values of instance variables or just flags for a new instance to read. You can’t store complex things here though. So things likeThreadorCursorinstances will not be appropriate. Basically anything that is not “data.”In the case where the activity is destroyed or even the process killed, then yes you will need to rebind and reinflate everything. However the beauty of this, in most cases you don’t have to do anything special. Only in the cases that you may have written to a bundle in
onSaveInstanceState()will you have to do some extra work.If the transition was only between onPause and onResume then yes. Everything should be fine. If there was a destroy or process kill, the activity will restart with
onCreate()and have to reset all the state based on theBundle savedInstanceState. I will address sqlite and files later down.Usually if they have
saveEnabled(true)(most do) and forTextViewthere is a freezesText property that will make them remember the last text set on them. However, most of the time if you save your state correctly during theonSaveInstanceState()call, it’s probable that you are storing the state not just for UI but other means as well. In which case you might as well sync them when you go through the nextonCreate()So
onPause()andonStop()occur on the main UI thread. If you are currently running code on the UI thread, it must complete before these other callbacks can occur. If you are running on some other thread, then yes you have to try to make outputs of task complete on the UI thread. It simplifies a lot of this.Kills on the other hand, can happen. This usually affects things outside your program’s memory though when we talk about corruption. Like if you had a file handle open or a sqlite cursor open, or a network socket open. With that, you sort of have to check the state before you use it.
i.e
hopefully this helps, while it’s not fully complete, it’s a good starting approach.