简体   繁体   English

在ViewPager下销毁的片段会发生什么情况?

[英]What happens to fragments destroyed under ViewPager?

I have a ViewPager that has 7 pages, and I am retaining 4 of them while navigating and it gets destroyed after being replaced by another 4 set then GC. 我有一个包含7页的ViewPager,在浏览时我保留了其中的4页,在被另外4组然后由GC替换后,它被破坏了。

 gPager.get().setOffscreenPageLimit(4);

Inside my custom FragmentStatePagerAdapter 在我的自定义FragmentStatePagerAdapter

@Override public Parcelable saveState() { return null; }

My problem is I am getting null pointer error even though I manually reinitialize the views in the Fragment after it is being destroyed like 'mDrawerLayout' and mDrawerList . 我的问题是我得到空指针错误,即使我手动重新初始化意见的片段也被破坏,如“mDrawerLayout”和后mDrawerList Example: 例:

        View rootView = inflater.inflate(R.layout.drawer_layout_fragment, container, false);
        mDrawerLayout = (DrawerLayout) rootView.get().findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) rootView.get().findViewById(R.id.left_drawer);

If the other fragments are destroyed and some are being displayed on the screen in ViewPager's pages. 如果其他片段被破坏,并且某些片段正在ViewPager页面的屏幕上显示。 Are these fragments recreated again or just being retained even it undergoes onDestory ? 这些片段是重新创建还是在经历onDestory被保留?

If Frag2 is destroyed and Frag1 is not. 如果Frag2被销毁而Frag1未被销毁。 Does the viewPager(located in the Main Activity) will call again the setAdapter ? viewPager(位于“主活动”中)是否会再次调用setAdapter Or it will automatically skip the setAdapter and go directly to MyCustomPagerAdapter to populate the fragment destroyed? 还是会自动跳过setAdapter并直接转到MyCustomPagerAdapter填充破坏的片段?

@Override @Override

public Fragment getItem(int arg0) {
    SavePref saveState = new SavePref(mContext.get());

    switch(arg0){
        /** Android tab is selected */
        case 0:
            Frag f1 = new Frag1()
            return f1;

        case 1:
            Frag f2 = new Frag2()
            return f2;
    }
}

一旦视图分页器销毁了片段,它将使用位置和数组进行重新创建,同时使用InstantiateItem ..中的逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ViewPager碎片会随着时间的推移而被破坏? - ViewPager Fragments getting destroyed over time? ViewPager还是片段? - ViewPager or Fragments? findfragmentbyid在mainPctivity中在viewpager下托管3个片段时返回null - findfragmentbyid returns null in mainActivity hosting 3 fragments under a viewpager 在创建它的Activity被销毁后,Android线程会发生什么? - What happens to an Android thread after the Activity that created it is destroyed? 在Android OS销毁某个活动之后,对活动的引用会如何处理? - What happens to references to an activity after it has been destroyed by the Android OS? 当Activity在其AsyncTask运行时被销毁时,内存中会发生什么? - What happens in memory when the Activity is destroyed while its AsyncTask is running? 捕捉和抛出一个例外:“引擎盖下”会发生什么? - Catching and Throwing an Exception: What happens “under the hood”? 代号在引擎盖下发生了什么 - Codename One what happens under the hood 当片段在Android中对用户可见时,在ViewPager片段上调用方法的正确方法是什么 - What is the correct way to call a method on ViewPager fragments when fragments become visible to user in android 隐藏带有片段的ViewPager - ViewPager with Fragments are Hidden
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM