Hello Android Developer,
i follow the Display Images Guide from Android Developers and want to load my initialized cache from a RetainFragment
if i rotate my device. A do this in my ListFragments
onActivityCreated()
method but the fragment is every time new created and not reused.
I do something like this: RetainFragment
class RetainFragment extends Fragment {
private static final String TAG = "RetainFragment";
public LruCache mRetainedCache;
public RetainFragment() {}
public static RetainFragment findOrCreateRetainFragment(FragmentManager fm) {
RetainFragment fragment = (RetainFragment) fm.findFragmentByTag(TAG);
if (fragment == null) {
fragment = new RetainFragment();
}
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
}
And this: ListFragment
@Override
public void onActivityCreated( Bundle savedInstanceState )
{
super.onActivityCreated( savedInstanceState );
RetainFragment mRetainFragment = RetainFragment.findOrCreateRetainFragment(getFragmentManager());
LruCache mMemoryCache = RetainFragment.mRetainedCache;
if (mMemoryCache == null)
{
mMemoryCache = new LruCache(cacheSize);
mRetainFragment.mRetainedCache = mMemoryCache;
}
else
{
mMemoryCache = mRetainFragment.mRetainedCache;
}
...
}