Sorry this is a rather long post, let me first try to explain the background:
I read a lot of posts on this topic (and Alex is a great blog post on this topic), and the general conclusion seems to be to not execute fragment transactions in an asynchronous callback (see Dianne post ) like AsyncTask#onPostExecute()
.
However, I have 2 cases where this is necessary:
Activity
showing the Fragment
login, when the user presses the login button, AsyncTask
starts authentication from the server, then when the login is returned, the login Fragment
is replaced with the Fragment
Main application.An Activity
, showing the main fragment of the application, when the user starts an action that requires logging in, the login fragment replaces the main fragment that is added to the backstack. Again, when the login button is pressed, AsyncTask
authenticates with the server, then when the login is successful, we want to pop back to show the main Fragment
user and let them perform the action that they wanted to perform.
Case 1 can be resolved using commitAllowingStateLoss
, but Case 2 is complicated because there is no such taste for popBackStack in the FragmentManager
.
In any case, both of these cases require special handling of the application going into the background during AsyncTask#doInBackground()
, calling onPostExecute()
when the application is in the background. One solution is to use Fragment.isResumed to protect the replacement of the fragment or pop-stop, and then process the process killed from the situation by logging in again or saving some flag indicating a recent successful login and replacing / appearing the login fragment in the state application recovery (login Fragment
restored up to the FragmentManager
). Or, resolve the loss of state and process the process, and then restore the situation, check the recent login and delete the login fragment.
How do you handle this? It seems like a lot of work to deal with a very common situation.
source share