简体   繁体   English

包含ViewPager的片段的生命周期

[英]Lifecycle of Fragment that holds a ViewPager

I have something that has me stumped. 我有些东西让我感到难过。 I have a Fragment (Fragment A) that has ViewPager that contains three fragments (for swiping left/right). 我有一个片段(片段A),它的ViewPager包含三个片段(用于向左/向右滑动)。 So, if within another fragment, in the onBackPressed() method, I do a getFragmentManager().popBackStack() call, Fragment A will be again be visible (with the ViewPager of sub-fragments) which is the desired state. 因此,如果在另一个片段中,在onBackPressed()方法中,我进行了getFragmentManager()。popBackStack()调用,则片段A将再次可见(带有子片段的ViewPager),这是所需的状态。 However, there is no method with Fragment A or within the ViewPager that indicates that Fragment A/ViewPager is again visible. 但是,没有使用片段A或ViewPager中的方法来指示片段A / ViewPager再次可见。

None of the fragment methods referenced in the Fragment lifecycle ( http://developer.android.com/guide/components/fragments.html ) that should be called when "The fragment returns the layout from the back stack" or any of the methods called within OnPageChangeListener (yes, I do call viewPager.setOnPageChangeListener(this) within Fragment A's onCreateView). 片段生命周期( http://developer.android.com/guide/components/fragments.html )中引用的所有片段方法均不应在“片段从后堆栈返回布局”时调用,也不应使用任何方法在OnPageChangeListener中调用(是的,我确实在片段A的onCreateView中调用viewPager.setOnPageChangeListener(this))。

Thoughts on where I could look? 关于我在哪里可以看到的想法?

I have an app with a ViewPager that is added to the layout, then later replaced by another fragment, with the change added to the transaction back stack. 我有一个带有ViewPager的应用程序,该应用程序被添加到布局中,然后被另一个片段替换,并将更改添加到事务返回堆栈中。 I have Log statements in each of the lifecycle methods of the pager. 我在寻呼机的每个生命周期方法中都有Log语句。 When the Back button is pressed and the pager is returned to the layout, my logcat output shows these methods called for the pager: onCreateView() , onActivityCreated() , onStart() , onResume() . 当按下“后退”按钮并将分页器返回到布局时,我的logcat输出显示了为分页器调用的以下方法: onCreateView()onActivityCreated()onStart()onResume() Note that when a fragment goes into the back stack, its view is destroyed, but the fragment object is not destroyed, so when the fragment returns from the back stack, there is no call to onCreate() . 请注意,当片段进入后堆栈时,其视图将被破坏,但片段对象不会被破坏,因此,当片段从后堆栈返回时,不会调用onCreate()

This behavior is consistent with the lifecycle diagram in the Fragment Guide . 此行为与“ 片段指南”中的生命周期图一致。 You should be able to use the call to onResume() as an indication that your pager is visible. 您应该能够使用对onResume()的调用来表明您的寻呼机可见。 I can only suggest that you add debug output to the lifecycle methods for your pager and look at the output. 我只能建议您将调试输出添加到寻呼机的生命周期方法中,然后查看输出。 If you think its wrong, please add it to the post of your question. 如果您认为这是错误的,请将其添加到问题的帖子中。 Also indicate what fragment transaction method(s) you are using. 同时指出您正在使用哪种片段事务处理方法。 If by chance you are using hide() instead of remove() or replace() , then the lifecycle events are different and you may need to use onHiddenChanged() . 如果您偶然使用了hide()而不是remove()replace() ,那么生命周期事件就不同了,您可能需要使用onHiddenChanged()

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

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