Working with onPause, onRestart, in Tab Widget - Android
-
29-09-2019 - |
Question
I have a ListView
that is inside a TabWidget
. When I select an item on the ListView
and go to the child ListView
, the TabWidget
disappears. This is fine, except that it invokes the onPause
method, and thus causes onRestart
to be called when I return to the parent ListView
.
I have onRestart
setup to retrieve updated data from the server, but I do not want this to occur everytime the user returns to the parent ListView
. I only want onRestart
to be called when the app comes alive from running in the background. I have tried implementing a Boolean variable to determine if I should execute the code that is inside onRestart
, but there doesn't seem to be a way to get around the effects of this.
Ideas?
Solution
You could fire off your child activity with startActivityForResult
and set a flag in onActivityResult
to not reload (which should be called when the user backs into the listview from the detail page). You'd have a member variable (let's say mReload
) that you'd set to true
in onCreate
and onRestart
, and to false
in onActivityResult
, then process the reload onResume
if mReload
is true (and set it back to false
so a normal resume won't trigger the reload).
Alternative: just set a flag (mLeftPage
) in your activity on the row's click listener. On restart, if mLeftPage
is true, don't reload the list and set mLeftPage
back to false. Otherwise, reload.