gpt4 book ai didi

java - 如何使用 Dagger2 将 FragmentPagerAdapter 正确注入(inject)我的 Activity 类?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:19:50 28 4
gpt4 key购买 nike

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

我的 Activity 课是:

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 时,您需要非常小心以确保您注入(inject)的成员跟踪 lifecycles注入(inject)目标的正确性。

ViewPagerAdapter 依赖于 FragmentManager。 FragmentManager 不是应用程序单例。单个 FragmentManager 将在 onCreate() 之后的某个时间对 Activity 可用,并且将仅遵循该 Activity 的生命周期。换句话说,当该 Activity 被销毁时,FragmentManager 将无法使用。如果您之后通过将其设为单例来保留对它的引用,那么您将遇到内存泄漏和上述错误,因为与其关联的 Activity 将不再存在。

处理这个问题的正确方法是创建一个新组件来跟踪您的 Activity 的生命周期:

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

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

@Module
public class TabActivityModule {

private final TabActivity activity;

public TabActivityModule(TabActivity activity) {
this.activity = activity;
}

@Provides
@PerActivity
FragmentManager fragmentManager() {
return activity.getFragmentManager();
}
}

关于java - 如何使用 Dagger2 将 FragmentPagerAdapter 正确注入(inject)我的 Activity 类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41299164/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com