Class FragmentGetContextFix


  • public final class FragmentGetContextFix
    extends java.lang.Object
    Runtime flag for the Fragment.getContext() fix. See https://github.com/google/dagger/pull/2620 for this change. Controls if fragment code should use the fixed getContext() behavior where it correctly returns null after a fragment is removed. This fixed behavior matches the behavior of a regular, non-Hilt fragment and can help catch issues where a removed or leaked fragment is incorrectly used.

    In order to set the flag, bind a boolean value qualified with FragmentGetContextFix.DisableFragmentGetContextFix into a set in the SingletonComponent. A set is used instead of an optional binding to avoid a dependency on Guava. Only one value may be bound into the set within a given app. If this is not set, the default is to not use the fix. Example for binding the value:

    
     @Module
     @InstallIn(SingletonComponent.class)
     public final class DisableFragmentGetContextFixModule {
       @Provides
       @IntoSet
       @FragmentGetContextFix.DisableFragmentGetContextFix
       static Boolean provideDisableFragmentGetContextFix() {
         return // true or false depending on some rollout logic for your app
       }
     }
     
    • Method Detail

      • isFragmentGetContextFixDisabled

        public static boolean isFragmentGetContextFixDisabled​(android.content.Context context)