编程删除ViewPager,当(或者我怎么能保证)载片段被破坏? [英] Programmatically removing a ViewPager, when are (or how can I ensure) contained Fragments are destroyed?

查看:134
本文介绍了编程删除ViewPager,当(或者我怎么能保证)载片段被破坏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经搜查事先就这个问题,我可以找到有关动态地添加和删除多次讨论从 ViewPager 片段取值C>。什么我其实关心这里不过是我如何通过编程删除整个 ViewPager 从含有的ViewGroup ,当 ViewPager 已被用来显示片段取值通过 FragmentPagerAdapter ,并保证所包含的片段 s的正确销毁。

要就这个问题展开多一点,我有一个景观双面板布局,其中一个选择是从列表的左侧,手工制作中的片段一边,选择内容,然后放置在在的FrameLayout 的权利。关键的一点是,该内容可能会或可能不会进行分页。因此,内容必须被显示在 ViewPager 格式,或者如果它没有分页则应当重新由psented $ P $一个片段直接。

要显示一个片段,我只是执行 FragmentTransaction ,你通常会以放置片段的FrameLayout 容器。如果另一方面它要显示分页内容,然后,而不是创建一个 ViewPager ,并将其添加作为一个子的FrameLayout

当我需要改变的内容,那么如果previous内容是一个独立的片段那么我可以简单地通过 FragmentTransaction 卸下摆臂()。当我这样做时,片段穿过的onPause() ... 的onDestroy( )周期预期。如果previous内容是一个 ViewPager 然后我从中删除了的FrameLayout 使用 .removeAllViews()。在这里,我来这个问题:我没有看到任何的的onPause() ... 的onDestroy()方法被调用任何的片段秒内即已经举行的 ViewPager 通过 FragmentPagerAdapter

从用户的角度来看,应用程序工作正常。经过几轮 ViewPager 被删除的,我可以看到GC回收内存。但是,我不喜欢这样的事实,那些片段 s的生活方法到底是不是叫我不能在他们做任何清理,它只是没有按T看起来正确的。

有没有一种方法,我可以挂接到以删除 ViewPager 片段 S当在 ViewPager 从其父分离,也许?换句话说,当我知道了的ViewGroup 不再使用,我将执行 FragmentTransaction 的某个地方(或许在 FragmentPagerAdapter ),以消除那些片段秒。

另外,我意识到,我可以只保留 ViewPager 在右侧永久,并动态交换片段秒内它。当然,它根本就没有关系,在某些时候,它只会保留一个页面。如果这会是一个更好的方式去,然后我将修改我的code要做到这一点,但我AP preciate意见。

解决方案
  

不过,我不喜欢这样的事实,那些碎片的生活方法到底是不是叫我不能在他们做任何清理,它只是似乎没有正确的。

他们应该得到清理活动时被破坏,如果不是已经太晚了(例如,堆的问题)。

  

在换句话说,当我知道的ViewGroup不再使用的,我会在某处(或许在FragmentPagerAdapter)执行FragmentTransactions消除这些片段。

您没有执行的交易把碎片那里。因此,你不能轻易执行事务删除片段。如果切换到 FragmentStatePagerAdapter ,并调用 setAdapter(空),它应该引起所有现有的片段在寻呼机被摧毁,我的阅读源$ C ​​$ C。 FragmentPagerAdapter 从不使用删除(),而 FragmentStatePagerAdapter 呢,从 destroyItem()方法,所有现存的片段通过 destroyItem()当一个新的适配器(或销毁)提供给 setAdapter()

Having searched regarding this issue beforehand, I can find many discussions regarding dynamically adding and removing selected Fragments from a ViewPager. What I'm actually concerned about here however is how I can programmatically remove an entire ViewPager 'cleanly' from its containing ViewGroup, when that ViewPager has been used to display Fragments via a FragmentPagerAdapter, and ensure that the contained Fragments are destroyed properly.

To expand on the question a bit more, I have a landscape two-pane layout where a selection is made from a list within a Fragment on the left-hand-side, and chosen content is then placed on the right within a FrameLayout. The key thing is that the content may or may not be paginated. Therefore, the content must either be displayed in a ViewPager format, or if it is not paginated then it shall be represented by a single Fragment directly.

To show a single Fragment, I simply perform a FragmentTransaction as you normally would in order to place the Fragment into the FrameLayout container. If on the other hand it's paginated content to be shown, then instead I create a ViewPager and add it as a child of the FrameLayout.

When I need to change the content, then if the previous content was a stand-alone Fragment then I can simply remove it via FragmentTransaction .remove(). When I do this, the Fragment goes through the onPause() ... onDestroy() cycle as expected. If the previous content was a ViewPager then I remove it from the FrameLayout using .removeAllViews(). Here I come to the problem: I don't see any of the onPause() ... onDestroy() methods being called in any of the Fragments that were held within that ViewPager via the FragmentPagerAdapter.

From a user point of view, the application works fine. After several rounds of ViewPager being removed, I can see the GC reclaiming memory. However, I don't like the fact that those Fragments' end of life methods aren't called as I can't do any cleanup within them, and it just doesn't seem 'right'.

Is there a method I can hook into in order to remove the ViewPager's Fragments when the ViewPager is detached from its parent, perhaps? In other words, when I know that the ViewGroup is no longer in used, I would perform FragmentTransactions somewhere (perhaps in the FragmentPagerAdapter) to remove those Fragments.

Alternatively, I realise that I could just keep the ViewPager on the right permanently, and dynamically swap the Fragments within it. Of course it simply would not matter that at certain times it would only hold one page. If this would be a better way to go then I shall refactor my code to do this, but I would appreciate opinions.

解决方案

However, I don't like the fact that those Fragments' end of life methods aren't called as I can't do any cleanup within them, and it just doesn't seem 'right'.

They should get cleaned up when the activity is destroyed, if that is not too late for you (e.g., heap issues).

In other words, when I know that the ViewGroup is no longer in used, I would perform FragmentTransactions somewhere (perhaps in the FragmentPagerAdapter) to remove those Fragments.

You did not execute the transactions to put the fragments there. Hence, you cannot readily execute the transactions to remove the fragments. If you switch to FragmentStatePagerAdapter, and call setAdapter(null), it should cause all existing fragments in the pager to be destroyed, by my reading of the source code. FragmentPagerAdapter never uses remove(), but FragmentStatePagerAdapter does, from its destroyItem() method, and all extant fragments are destroyed via destroyItem() when a new adapter (or null) is supplied to setAdapter().

这篇关于编程删除ViewPager,当(或者我怎么能保证)载片段被破坏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆