Background

Asynchronous Callbacks in Android

Trying to perform an asynchronous operation in a reliable fashion on Android is unnecessarily convoluted i.e. Is AsyncTask really conceptually flawed or am I just missing something?

Now, this is all prior to the introduction of Fragments. With the introduction of Fragments, onRetainNonConfigurationInstance() has been deprecated. So the latest Google condoned hack is to use a persistent non-UI fragment that attaches/detaches from your Activity when configuration changes occur (i.e. Rotating the screen, changing language settings etc.)

Example: https://code.google.com/p/android/issues/detail?id=23096#c4

IllegalStateException - Can not perform this action after onSaveInstanceState

Theoretically the hack above allows you to get around the dreaded:

IllegalStateException - "Can not perform this action after onSaveInstanceState"

because a persistent non-UI fragment will receive callbacks for onViewStateRestored() (alternatively onResume) and onSaveInstanceState() (alternatively onPause). As such you can tell when the instance state is saved/restored. It's a fair bit of code for something so simple, but utilising this knowledge, you could queue up your asynchronous callbacks until the activity's FragmentManager has its mStateSaved variable set to false before executing them.

mStateSaved being the variable who is ultimately responsible for firing this exception.

private void checkStateLoss() {
    if (mStateSaved) {
        throw new IllegalStateExcept
Edit
Report