如何摧毁旧片段FragmentStatePagerAdapter [英] How to destroy old fragments in FragmentStatePagerAdapter

查看:175
本文介绍了如何摧毁旧片段FragmentStatePagerAdapter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现这一点:
我使用的是ViewPager与FragmentStatePagerAdapter。
我开始从该页面的例子:
<一href="http://developer.android.com/reference/android/support/v4/app/FragmentStatePagerAdapter.html">http://developer.android.com/reference/android/support/v4/app/FragmentStatePagerAdapter.html

I want to implement this:
I use a ViewPager with a FragmentStatePagerAdapter.
I started with the example from this page:
http://developer.android.com/reference/android/support/v4/app/FragmentStatePagerAdapter.html

这是我ViewPager适配器:

This is my ViewPager adapter:

    public static class MyAdapter extends FragmentStatePagerAdapter {
        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return NUM_ITEMS;
        }

        @Override
        public Fragment getItem(int position) {
            return ArrayListFragment.newInstance(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            super.destroyItem(container, position, object);
        }
    }

我ViewPager的每个页面包含一些数据的ListView控件。 在那一刻,我切换到一个新的一页ViewPager它会很快增加RAM内存。
我应该如何删除旧的碎片?
我也用这个,但它什么都不做:

Every page of my ViewPager contains a ListView with some data. At the moment when I switch to a new page in ViewPager it will increase the RAM memory very quickly.
How should I remove the old fragments ?
I also used this but it does nothing:

public void destroyItem(ViewGroup container, int position, Object object) {

    FragmentManager manager = ((Fragment) object).getFragmentManager();
    FragmentTransaction trans = manager.beginTransaction();
    trans.remove((Fragment) object);
    trans.commit();

    super.destroyItem(container, position, object);
}

也有1-2秒的延迟后,我迅速切换到一个新的页面或旧的一页。是否有任何技术消除了延迟。如果我切换到一个新的页面,等待2秒钟,然后下一个交换机上没有更多的延迟​​。

There is also a 1-2 seconds delay after I switch quickly to a new page or old page. Is there any technique to remove that delay. If I switch to a new page and wait for 2 second then on next switch there is no more delay.

通过Nexus 7。

推荐答案

你不应该试图干扰与Android如何管理你的片段的实施。默认为 setOffScreenPageLimit 应该已经是一个。这意味着Android将摧毁旧的片段,当内存不足。只要你没有内存问题,只要把它定。

You should not try to interfere with how Android manages your Fragment implementations. The default for the setOffScreenPageLimit should already be one. This means that Android will destroy old fragments when memory runs low. As long as you do not have a memory issue, just leave it be.

之所以你的内存的增加是由于Android保持片段实例在内存中能够重新连接,而不必实例化它们给他们。我建议你​​考虑你的片段实例的操作系统被破坏的应变,从而节省自己的状态,如果出现这种情况,而让操作系统做它的工作。

The reason why your memory increases is because Android keeps Fragment instances in memory to be able to reconnect to them instead of having to instantiate them. I recommend you account for the contingency of your Fragment instances being destroyed by the OS, saving their state if that happens, and let the OS do its job.

您所遇到的延迟可能是由于在UI线程上的一些密集的计算。如果是,我建议把那出来,例如,的AsyncTask 。如果没有code是,但是,只是一个猜测,什么可能导致此问题。但是,只能是一个初始延迟建议您加载一些东西,可能会阻塞UI线程。

The delay you are experiencing could be due to some intensive computation on the UI thread. If it is, I suggest moving that out to, for example, an AsyncTask. Without the code it is, however, just a guess as to what might cause the issue. But there being only an initial delay suggests that you are loading something which might block the UI thread.

更新:有一个在 http://stackoverflow.com/a/9646622/170781 < /一>,概述很整齐如何使用 ViewPager 手柄片段实例。

Update: Have a look at http://stackoverflow.com/a/9646622/170781 which outlines very neatly how the ViewPager handles Fragment instances.

这篇关于如何摧毁旧片段FragmentStatePagerAdapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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