Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Activity to restore the safe Loaders behavior of support libraries < 24.0.0 in recent versions
* Inherit from this class to prevent Loaders from being forcefully retained during a configuration change.
* Forceful retain currently causes all stopped Loaders to briefly start, causing unexpected issues for detached fragments.
* This restores the Loaders behavior of support libraries < 24.0.0
* @author Christophe Beyls
* @see <a href="">Bug report</a>
public class SafeLoadersFragmentActivity extends FragmentActivity {
void doReallyStop(boolean retaining) {
// Ensure the Activity Loaders are only stopped once and never restarted during stop
if (!mReallyStopped) {
public Object onRetainCustomNonConfigurationInstance() {
// All loaders are already stopped or retained at that point, but calling this method again
// sets a flag to prevent them from being forcefully retained during the next phase
return super.onRetainCustomNonConfigurationInstance();
* Hack to force update the LoaderManager in RETAINED fragments
* in order to avoid memory leaks and Loaders malfunction. Call this in Fragment.onAttach().
public static void updateLoaderManager(Fragment fragment) {
fragment.mLoaderManager = null;
fragment.mCheckedForLoaderManager = false;

This comment has been minimized.

Copy link
Owner Author

@cbeyls cbeyls commented Apr 27, 2019

Deprecated. Please migrate to LiveData and ViewModel from architecture components.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment