Skip to content Skip to sidebar Skip to footer

Retaining Asynctask From Activity, Not Fragment

So.. What I have is as follows, a complicated fragment based viewpager utilizing tabhosts etc with my activity. This is the basic setup. Once this has been created, I use an AsyncT

Solution 1:

You can use an invisible Fragment to retain your state. It's essentially the same as onRetainNoneConfigurationInstance but not deprecated.

see http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html#config-changes

There is a bug in the example and you have to add the RetainFragment to the FragmentManager or it's not going to be restored.

publicstatic RetainFragment findOrCreateRetainFragment(FragmentManager fm) {
    RetainFragment fragment = (RetainFragment) fm.findFragmentByTag(TAG);
    if (fragment == null) {
        fragment = new RetainFragment();
        fm.beginTransaction().add(fragment, TAG).commit(); // add this
    }
    return fragment;
}

Solution 2:

Use an IntentService to get data from the database, and you don't have to worry about configuration changes. Send an Intent to get the data, use LocalBroadcastManager to send a broadcast Intent back to any other component that you want to notify.

Post a Comment for "Retaining Asynctask From Activity, Not Fragment"