数据丢失解决方法:
其实是Fragment嵌套了Fragment的问题,外层的Fragment是调用
NewMainPagerAdapter adapter = new NewMainPagerAdapter(getFragmentManager(), viewpagerFragmentList, mTitles);
而内层是要调用
BannerPagerAdapter mBannerPagerAdapter = new BannerPagerAdapter(fragment, mNewsMain.banners);
public BannerPagerAdapter(Fragment fm, List<NewsMain.Banner> banner) {
super(fm.getChildFragmentManager());
mBanner = banner;
}
修改完成后数据不会丢失了,但是来回切换几下,又会crash,报错如下
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: FATAL EXCEPTION: main
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: java.lang.NullPointerException
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: at android.app.FragmentState.instantiate(Fragment.java:94)
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: at android.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1747)
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: at android.app.Fragment.performCreate(Fragment.java:1684)
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:854)
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: at android.app.BackStackRecord.run(BackStackRecord.java:682)
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: at android.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:474)
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: at android.support.v13.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:167)
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: at android.support.v4.view.ViewPager.populate(ViewPager.java:1105)
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:551)
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:513)
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:494)
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: at android.support.design.widget.TabLayout$ViewPagerOnTabSelectedListener.onTabSelected(TabLayout.java:1584)
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: at android.support.design.widget.TabLayout.selectTab(TabLayout.java:807)
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: at android.support.design.widget.TabLayout.selectTab(TabLayout.java:779)
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: at android.support.design.widget.TabLayout$Tab.select(TabLayout.java:1039)
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: at android.support.design.widget.TabLayout$1.onClick(TabLayout.java:620)
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: at android.view.View.performClick(View.java:4204)
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:17355)
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:725)
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:92)
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137)
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5041)
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
解决方法:
重写嵌套Fragment的onDetach方法(即调用了
getChildFragmentManager()
的Fragment
)
@Override
public void onDetach() {
super.onDetach();
try {
Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
childFragmentManager.setAccessible(true);
childFragmentManager.set(this, null);
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}