Dagger 2 on Android: inject same dependency in Activity and retained Fragment

后端 未结 1 1038
一整个雨季
一整个雨季 2021-02-05 12:36

I have objects of classes F1 and F2 that I want to inject in a retained Fragment. I also have an object of class A that depends on Activit

相关标签:
1条回答
  • 2021-02-05 12:45

    Considering your retained fragment lives longer than your activity, I'd wager that the proper way to do this would be to make the FragmentScope contain the ActivityScope, and not vice versa.

    Meaning your FragmentComponent would have

    @FragmentScope
    @Component(modules = {FragmentModule.class})
    public interface FragmentComponent {
        void inject(MyFragment presenter);
    }
    

    And your Activity component would have

    @ActivityScope
    @Component(dependencies = {FragmentComponent.class}, modules = {MainActivityModule.class})
    public interface MainActivityComponent extends FragmentComponent { //provision methods
        void inject(MainActivity activity);
    
        // make the A object available to dependent components
        A getA();
    }
    

    Which is possible if your Fragment injected classes don't rely on the Activity module as dependencies.

    This can be done with something akin to

    public class MainActivity extends AppCompatActivity {
    
        private MainActivityComponent mainActivityComponent;
    
        private MyFragment myFragment;
    
        @Override
        public void onCreate(Bundle saveInstanceState) {
             super.onCreate(saveInstanceState);
             setContentView(R.layout.activity_main);
    
             if(saveInstanceState == null) { // first run
                 myFragment = new MyFragment(); //headless retained fragment
                 getSupportFragmentManager()
                    .beginTransaction()
                    .add(myFragment, MyFragment.class.getName()) //TAG
                    .commit();
             } else {
                 myFragment = (MyFragment)(getSupportFragmentManager()
                                   .findFragmentByTag(MyFragment.class.getName()));
             }
        }
    
        @Override
        public void onPostCreate() {
             mainActivityComponent = DaggerMainActivityComponent.builder()
                  .fragmentComponent(myFragment.getComponent())
                  .build();
        }
    }
    

    And

    public class MyFragment extends Fragment {
        public MyFragment() {
             this.setRetainInstance(true);
        }
    
        private FragmentComponent fragmentComponent;
    
        @Override
        public void onCreate(Bundle saveInstanceState) {
            super.onCreate(saveInstanceState);
            this.fragmentComponent = DaggerFragmentComponent.create();
        }
    
        public FragmentComponent getFragmentComponent() {
            return fragmentComponent;
        }
    }
    

    EDIT:

    public class MyFragment extends Fragment {
        public MyFragment() {
             this.setRetainInstance(true);
             this.fragmentComponent = DaggerFragmentComponent.create();
        }
    
        private FragmentComponent fragmentComponent;
    
        public FragmentComponent getFragmentComponent() {
            return fragmentComponent;
        }
    }
    
    public class MainActivity extends AppCompatActivity {
    
        private MainActivityComponent mainActivityComponent;
    
        private MyFragment myFragment;
    
        @Inject
        A mA;
    
        @Override
        public void onCreate(Bundle saveInstanceState) {
             super.onCreate(saveInstanceState);
             setContentView(R.layout.activity_main);
    
             if(saveInstanceState == null) { // first run
                 myFragment = new MyFragment(); //headless retained fragment
                 getSupportFragmentManager()
                    .beginTransaction()
                    .add(myFragment, MyFragment.class.getName()) //TAG
                    .commit();
             } else {
                 myFragment = (MyFragment)(getSupportFragmentManager().findFragmentByTag(MyFragment.class.getName()));
             }
             mainActivityComponent = DaggerMainActivityComponent.builder()
                  .fragmentComponent(myFragment.getComponent())
                  .build();
             mainActivityComponent.inject(this);
             mA.onCreate();
        }
    }
    
    0 讨论(0)
提交回复
热议问题