简体   繁体   English

片段在没有调用 onCreate 的情况下初始化

[英]Fragment initialized without onCreate being called

So I have a fragment inside a viewpager which is contained inside a fragment which is getting initialize'd as shown in the debugger but doesn't have it's onCreate,onCreateView or any such methods being called.所以我在 viewpager 中有一个片段,它包含在一个片段中,该片段正在初始化,如调试器所示,但没有 onCreate、onCreateView 或任何此类被调用的方法。 The activity containing has a bottom navigation view and contains 4 such fragments and this issue is only happening in 1 such fragment.包含的活动有一个底部导航视图,包含 4 个这样的片段,这个问题只发生在 1 个这样的片段中。

All these fragments and viewpagers and fragments inside them are created on the oncreate of the activity.所有这些片段和视图页面以及其中的片段都是在 Activity 的 oncreate 上创建的。 If I move the logic to create the fragment to when a bottom tab is clicked, this issue gets solved on.如果我将创建片段的逻辑移动到单击底部选项卡时,此问题将得到解决。

How is this possible ?这怎么可能 ?

Only the currently active fragment and the two directly adjecant fragments are created when the viewpager is first shown.首次显示 viewpager 时,只会创建当前活动的片段和两个直接相邻的片段。 so the fragment for the first page, the fragment for the page shown if you "scroll to the left" and the fragment for the page shown when you "scroll to the right".所以第一页的片段,“向左滚动”时显示的页面的片段,以及“向右滚动”时显示的页面的片段。 the 4th fragment will be created when it is put in the next adjecant position.将第 4 个片段放在下一个相邻位置时,将创建第 4 个片段。

so if you have fragments a,b,c,d in a viewpager like -a-[b]-cd- where b is the first visible page, only a,b and c will be created at startup.因此,如果您在像 -a-[b]-cd- 这样的视图分页器中有片段 a、b、c、d,其中 b 是第一个可见页面,则在启动时只会创建 a、b 和 c。 when you scroll to c -ab-[c]-d- onCreate for fragment d will be called.当您滚动到 c -ab-[c]-d- onCreate for fragment d 将被调用。

You can control number of pages/fragments preloaded when you launch your activity by setting it's off Screen Page Limit on your view pager adapter.您可以通过在视图寻呼机适配器上将其设置为关闭屏幕页面限制来控制启动活动时预加载的页面/片段数量。 like this像这样

  • mViewPager.setOffScreenPageLimit(limit) mViewPager.setOffScreenPageLimit(limit)

If you set limit to 0 than only first fragment would be load.如果您将限制设置为 0,则只会加载第一个片段。

https://spotandroid.com/2016/11/23/android-tricks-viewpager-and-offscreen-tabs/ https://spotandroid.com/2016/11/23/android-tricks-viewpager-and-offscreen-tabs/

https://techcodegeek.wordpress.com/2015/06/23/android-viewpager-and-performance-improvements/ https://techcodegeek.wordpress.com/2015/06/23/android-viewpager-and-performance-improvements/

需要更多详细信息才能了解您的问题。您所提供的信息无法理解您面临的问题。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM