如何使用Dagger2将FragmentPagerAdapter正确地插入我的活动类中?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用Dagger2将FragmentPagerAdapter正确地插入我的活动类中?相关的知识,希望对你有一定的参考价值。

我正在尝试制作一个提供ViewPagerAdapter的模块类AdaptersModules。 ViewPagerAdapter是扩展FragmentPagerAdapter的类。 ViewPagerAdapter构造函数需要FragmentManager。

我的活动分类是:

public class TabsActivity extends AppCompatActivity 

public FragmentManager fm = getSupportFragmentManager();

@Inject ViewPagerAdapter adapter;
@Inject HomeFragment homeFragment;
@Inject ChallengeFragment challengeFragment;
@Inject FriendsFragment friendsFragment;
@Inject OptionsFragment optionsFragment;
@Inject QuickPhotoFragment quickPhotoFragment;
@Inject TrophiesFragment trophiesFragment;


@BindView(R.id.toolbar) Toolbar toolbar;
@BindView(R.id.tabsTabLayout) TabLayout tabLayout;
@BindView(R.id.tabsViewPager) ViewPager viewPager;

private int[] tabIcons =   R.drawable.ic_home,
                            R.drawable.ic_quick_photo,
                            R.drawable.ic_challenge,
                            R.drawable.ic_friends,
                            R.drawable.ic_trophies,
                            R.drawable.ic_settings  ;

@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tabs);
    ButterKnife.bind(this);
    ((MeetBamApplication)getApplication())
            .getComponent()
            .inject(this);


    setSupportActionBar(toolbar);
    getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);

    setupViewPager(viewPager);

    tabLayout.setupWithViewPager(viewPager);
    for (int i=0; i<viewPager.getAdapter().getCount(); i++)
        tabLayout.getTabAt(i).setIcon(tabIcons[i]);


private void setupViewPager(ViewPager viewPager) 

    adapter.addFragment(homeFragment);
    adapter.addFragment(quickPhotoFragment);
    adapter.addFragment(challengeFragment);
    adapter.addFragment(friendsFragment);
    adapter.addFragment(trophiesFragment);
    adapter.addFragment(optionsFragment);
    viewPager.setAdapter(adapter);


我的模块类别:

 @Module
 public class AdaptersModules extends TabsActivity

@Singleton
@Provides
protected ViewPagerAdapter provideViewPagerAdapter()
    return new ViewPagerAdapter(fm);


和组件:

 @Singleton
 @Component(modules = 
         AppModule.class,
         TabsModules.class,
         AdaptersModules.class)
 public interface ApplicationComponent 

    void inject(MeetBamApplication meetBamApplication);
    void inject(TabsActivity activity);

ViewPagerAdapter类是:

public class ViewPagerAdapter extends FragmentPagerAdapter 

     private final List<Fragment> mFragmentList = new ArrayList<>();

     public void addFragment(Fragment fragment) mFragmentList.add(fragment);

     public ViewPagerAdapter(FragmentManager fm)  super(fm); 

     @Override
     public Fragment getItem(int position)  return mFragmentList.get(position); 

     @Override
     public int getCount()  return mFragmentList.size(); 
 

当我运行并测试应用程序时,我得到:

 FATAL EXCEPTION: main
                                                                          Process: com.thomsoncompany.meetbamgo, PID: 23733
                                                                          java.lang.IllegalStateException: Activity has been destroyed
                                                                              at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1515)
                                                                              at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:638)
                                                                              at android.support.v4.app.BackStackRecord.commitAllowingStateLoss(BackStackRecord.java:621)
                                                                              at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:139)
                                                                              at android.support.v4.view.ViewPager.populate(ViewPager.java:1177)
                                                                              at android.support.v4.view.ViewPager.populate(ViewPager.java:1025)
                                                                              at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1545)
                                                                              at android.view.View.measure(View.java:17782)
                                                                              at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5625)
                                                                              at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:668)
                                                                              at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:735)
                                                                              at android.view.View.measure(View.java:17782)
                                                                              at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5625)
                                                                              at android.widget.FrameLayout.onMeasure(FrameLayout.java:459)
                                                                              at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
                                                                              at android.view.View.measure(View.java:17782)
                                                                              at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5625)
                                                                              at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1692)
                                                                              at android.widget.LinearLayout.measureVertical(LinearLayout.java:760)
                                                                              at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
                                                                              at android.view.View.measure(View.java:17782)
                                                                              at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5625)
                                                                              at android.widget.FrameLayout.onMeasure(FrameLayout.java:459)
                                                                              at android.view.View.measure(View.java:17782)
                                                                              at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5625)
                                                                              at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1692)
                                                                              at android.widget.LinearLayout.measureVertical(LinearLayout.java:760)
                                                                              at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
                                                                              at android.view.View.measure(View.java:17782)
                                                                              at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5625)
                                                                              at android.widget.FrameLayout.onMeasure(FrameLayout.java:459)
                                                                              at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2644)
                                                                              at android.view.View.measure(View.java:17782)
                                                                              at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2389)
                                                                              at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1399)
                                                                              at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1623)
                                                                              at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1277)
                                                                              at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6483)
                                                                              at android.view.Choreographer$CallbackRecord.run(Choreographer.java:802)
                                                                              at android.view.Choreographer.doCallbacks(Choreographer.java:605)
                                                                              at android.view.Choreographer.doFrame(Choreographer.java:574)
                                                                              at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:788)
                                                                              at android.os.Handler.handleCallback(Handler.java:815)
                                                                              at android.os.Handler.dispatchMessage(Handler.java:104)
                                                                              at android.os.Looper.loop(Looper.java:194)
                                                                              at android.app.ActivityThread.main(ActivityThread.java:5549)
                                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                                              at java.lang.reflect.Method.invoke(Method.java:372)
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:964)
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759)

我该怎么办?

答案

[当您在Android中使用Dagger 2时,需要非常小心以确保您注入的成员正确跟踪注入目标的lifecycles

ViewPagerAdapter取决于FragmentManager。 FragmentManager是应用程序单例。 FragmentManager之后的某个时间,单个FragmentManager将对Activity可用,并将仅遵循该Activity的生命周期。换句话说,当该活动被销毁时,onCreate()不可用。如果以后通过将其设为单例来保留对它的引用,则将发生内存泄漏和上述错误,因为与其关联的活动将不再存在。处理此问题的正确方法是制作一个新组件来跟踪您的Activity的生命周期:

FragmentManager

然后为您的活动创建一个模块,该模块可以提供需要跟踪该活动的生命周期的成员:

@PerActivity @Component( dependencies = ApplicationComponent.class , modules = TabsModule.class, AdaptersModule.class, TabActivityModule.class public interface TabComponent void inject(TabsActivtiy activity);

以上是关于如何使用Dagger2将FragmentPagerAdapter正确地插入我的活动类中?的主要内容,如果未能解决你的问题,请参考以下文章

Android单排上王者系列之Dagger2注入原理解析

如何将Dagger2的子组件用于GWT

Dagger2注入原理解析

无法使用 Dagger2 将对象注入 ViewModel 类

如何在仍然使用 Dagger2 的同时解决循环依赖?

如何从测试覆盖率中排除 Dagger2 类