机器人。如何保存自定义ArrayAdapters和ViewPagers内存 [英] android. How to conserve memory with custom ArrayAdapters and ViewPagers

查看:130
本文介绍了机器人。如何保存自定义ArrayAdapters和ViewPagers内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个视图寻呼机MainActivity。我创建用于查看传呼机这样的非静态定制FragmentPageAdapter:

So I have a MainActivity with a View Pager. I create a nonstatic custom FragmentPageAdapter for the View Pager like this:

public class CustomViewPageAdapter extends FragmentStatePagerAdapter {

适配器有4项(片段),我声明为全局变量MainActivity为静态的,所以例如:

The Adapter has 4 items(fragments) Which I declare as global variables MainActivity as static so for example:

    static HotFragment hotFragment;

然后我在FragmentStatePagerAdapter我的getItem()方法返回hotFragment。

Then I return hotFragment in my getItem() method of the FragmentStatePagerAdapter.

然后在我的HotFragment类我有另一个ViewPager以及另外FragmentStatePagerAdapter这也是非静态:在适配器的getItem()方法返回一个叫做ImageDetailFragment这样的另一片段的新实例:

Then in my HotFragment class I have another ViewPager as well as another FragmentStatePagerAdapter which is ALSO nonstatic: In the getItem() method of that Adapter return a new instance of another fragment called ImageDetailFragment like this:

return ImageDetailFragment.newInstance(arg0);

这ImageDetailFragment contatins一个ImageView的,这将取决于arg0中的价值持有不同的图像。该ImageView的被装入一个AsyncTask的是在MainActivity。异步持有的WeakReference到的ImageView以确保它是垃圾回收。

That ImageDetailFragment contatins an ImageView that will hold a different image depending on the value of arg0. That ImageView is loaded in an AsyncTask which is in the MainActivity. The async holds a WeakReference to the ImageView to ensure that it is Garbage collected.

我的问题是:
我经常收到与内存不足错误,当我滚动影像,尤其是当我退出该活动,然后再进入活动。我很新的内存管理和垃圾收集,但我想我有内存泄漏的地方。不,这不是一个却模糊的问题。我想知道的是:
我应该让我的hotFragment静态或不是因为我知道,静态对象可能导致内存泄漏。
也。我FragmentStatePagerAdapters都是非静态的是这个坏。如果所有适配器在片段静态的。
另一件事。在我的HotFragment我FragmentPagerAdapter的getItem(ID)的方法,我回:

My Problem is: I am often getting and OutOfMemory error when I scroll through the images, especially when I exit the activity and then re enter the activity. I am very new to memory management and garbage collection but I think I have a memory leak somewhere. No this isn't a vague question however. What I want to know is: Should I make my hotFragment static or not because I know static objects can cause memory leaks. Also. My FragmentStatePagerAdapters are nonStatic is this bad. Should all Adapters be static in fragments. Another thing. In my getItem(id) method in the FragmentPagerAdapter in my HotFragment I return:

ImageDetailFragment.newInstance();

,而不是在实际HotFragment创建ImageDetailFragment的一个实例。这是坏?因为在我MainActivity我不回的hotFragment我在MainActivity创建hotFragment的静态实例以同样的方式再返回。哪一个更好。

instead of creating an instance of ImageDetailFragment in the actual HotFragment. Is this bad? Because in my MainActivity I don't return the hotFragment the same way I create a static instance of the hotFragment in the MainActivity then return that. Which one is better.

我真的AP preciate,如果你们帮助了我。我是新来的内存管理和我不确定是什么,以避免内存泄漏的最佳实践。对不起,如果我的问题是太长或模糊。我真的试图使它尽可能描述可能的...谢谢

I would really appreciate it if you guys helped me. I am new to memory management and am unsure about what are the best practices to avoid memory leaks. I'm sorry if my question is too long or vague. I really tried to make it as descriptive as possible... Thank you

推荐答案

如果你真的相信有很多静片段在内存消耗大量的内存,也许你可以尝试这样的事:

If you are really sure that having lots of static fragments in memory consumes a huge amount of memory, maybe you could try something like this:

而不是使用的:

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class below).
        return PlaceholderFragment.newInstance(position);
    }

其实你可以声明地图片段。其主要目标是重新使用previously创建片段,比保持宣称的新实例的。首先,宣布该code对片段的活动(不在SectionsPagerAdapter):

You could actually declare a Map of fragment. The main goal is to re-use previously created fragments, than keep declaring new instance. Firstly, declare this code on fragment's activity (NOT in SectionsPagerAdapter):

private Map<Integer, PlaceholderFragment> mPlaceHolderFragmentArray = new LinkedHashMap<Integer, PlaceholderFragment>(); 

然后更换的getItem(INT位置)方法SectionsPagerAdapter本:

Then replace getItem(int position) method in SectionsPagerAdapter with this:

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class below).
        PlaceholderFragment fragment = mPlaceHolderFragmentArray.get(position);
        if(fragment == null){
            fragment = PlaceholderFragment.newInstance(position);
            mPlaceHolderFragmentArray.put(position, fragment);
        }
        return fragment;
    }

我不知道是否有什么更好的办法,但目前我使用这个对我的codeS。

I don't know if there are any better way, but I'm currently using this on my codes.

这篇关于机器人。如何保存自定义ArrayAdapters和ViewPagers内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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