Skip to content Skip to sidebar Skip to footer

Android Fragment Lifecycle Of Single Instance Activity

I have a singleInstance Activity and a Fragment which I instantiate in the onCreate() method and add into a FrameLayout container into the layout of the activity. The activity does

Solution 1:

I can't answer question 1, although I've noticed that behavior as well, but I rarely do much work in my onStop methods (I favor onPause and onResume), I can help with your 2nd question.

The problem here (which is definitely a google bug) is either a problem with FragmentActivity or the Activity lifecycle as a whole (depending on how you look at it).

Basically, the FragmentActivity moves its fragments to the resumed state, not in the onResume method (as a generally sane person might think) but in the onPostResume method. This is all well and good, except that the onPostResume method never gets called in a singleIntstance/singleTask activity when the activity is recalled with onNewIntent.

So what I did (having imported the support package code, instead of just the jar) was modify FragmentActivity like so...

//this boolean is only ever set to true in onNewIntentprivateboolean mResumeNeedsToDoDispatch = false; 

/**
 * Ensure any outstanding fragment transactions have been committed.
 */@OverrideprotectedvoidonResume() {
    super.onResume();
    mResumed = true;

    //Check if onNewIntent was called. If so, dispatch resumes to fragments nowif (mResumeNeedsToDoDispatch) {
        mResumeNeedsToDoDispatch = false;
        mFragments.dispatchResume();
    }

    mFragments.execPendingActions();
}


/**
 * Google, I think you forgot this #(
 */@OverridepublicvoidonNewIntent(Intent newIntent) {
    super.onNewIntent(newIntent);
    mResumeNeedsToDoDispatch = true;
}

Notice that I didn't just call mFragments.dispatchResume() in onNewIntent cause this causes the fragment onResume methods to be called twice in this case. I'm not 100% sure why that is, but that is what I noticed in my tests.

Hope this helps :)

Solution 2:

This led me to a related discovery --

I have a fragment that is inflated from an xml tag. On a Xoom running 3.2.1, things work as expected. On a Galaxy 10.1 running 3.1, the fragment's onResume method is never called. Looks like the fix may have been added in 3.2.

Solution 3:

I just want to add to Geoff's comment, in my particular case I was recreating a set of nested fragments when onNewIntent was fired, and for it to successfully work, I setup a class member mShouldUpdateFragments, setting it true in onNewIntent, and overriding onPostResume where I did my work based on the boolean.

Post a Comment for "Android Fragment Lifecycle Of Single Instance Activity"