Backstack和内存泄漏 [英] Backstack and memory leakage

查看:155
本文介绍了Backstack和内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开发了基于谷歌图书馆的支持非常简单的要求,对话的片段一个很小的API:


  • API可能会增加(或更换)模态对话框

  • API可以辞退编程对话框或用户可以驳回pressing按钮对话框

我的API通过不断增加碎片backstack创建内存泄漏?

 公共类DialogFragmentUtils {私有静态最后弦乐DIALOG_TAG =dialogTag;公共静态无效showDialogFragment(@Nullable活动活动,@NotNull片段片段){
    如果(的instanceof FragmentActivity活动){
        FragmentActivity fragmentActivity =(FragmentActivity)活性;
        FragmentManager FM = fragmentActivity.getSupportFragmentManager();
        FragmentTransaction英尺= fm.beginTransaction();
        片段preV = fm.findFragmentByTag(DIALOG_TAG);
        如果(preV =空&安培;!&安培; prev.isAdded()){
            ft.remove(preV);
        }
        ft.add(片段,DIALOG_TAG);
        ft.addToBackStack(NULL);
        ft.commit();
    }
}公共静态无效dismissDialogFragment(@Nullable活动活动){
    如果(的instanceof FragmentActivity活动){
        FragmentActivity fragmentActivity =(FragmentActivity)活性;
        FragmentManager FM = fragmentActivity.getSupportFragmentManager();
        DialogFragment对话框=(DialogFragment)fm.findFragmentByTag(DIALOG_TAG);
        如果(对话!= NULL){
            dialog.dismiss();
        }
    }
}
}


解决方案

是的,它容易产生低内存,没有内存泄漏虽然。所有回栈片段都保存在硬引用的内存。所以,如果你保持片段 S IN回栈的荒谬金额,然后你会得到内存不足。

有一个看看这里:<一href=\"http://stackoverflow.com/questions/8482606/when-a-fragment-is-replaced-and-put-in-the-back-stack-or-removed-does-it-stay\">When一个片段被换下,换上中后卫栈(或删除)它留在记忆?

更新:我看到你的 DIALOG_TAG 并没有改变,所以你只保留有一个片段在一个时间backstack,如果存在的话,因为你删除旧的。在这种情况下,你可能没有内存不足的问题。

I have developed a small API for dialog fragments based on Google support library with very simple requirements:

  • API could add (or replace) a modal dialog
  • API could dismiss dialog programmatically or user can dismiss dialog by pressing button

Does my API creates a memory leakage by constantly adding fragments to backstack?

public class DialogFragmentUtils {

private static final String DIALOG_TAG = "dialogTag";

public static void showDialogFragment(@Nullable Activity activity, @NotNull Fragment fragment) {
    if (activity instanceof FragmentActivity) {
        FragmentActivity fragmentActivity = (FragmentActivity) activity;
        FragmentManager fm = fragmentActivity.getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        Fragment prev = fm.findFragmentByTag(DIALOG_TAG);
        if (prev != null && prev.isAdded()) {
            ft.remove(prev);
        }
        ft.add(fragment, DIALOG_TAG);
        ft.addToBackStack(null);
        ft.commit();
    }
}

public static void dismissDialogFragment(@Nullable Activity activity) {
    if (activity instanceof FragmentActivity) {
        FragmentActivity fragmentActivity = (FragmentActivity) activity;
        FragmentManager fm = fragmentActivity.getSupportFragmentManager();
        DialogFragment dialog = (DialogFragment) fm.findFragmentByTag(DIALOG_TAG);
        if (dialog != null) {
            dialog.dismiss();
        }
    }
}
}

解决方案

Yes, its prone to low memory, not memory leak though. All back stack Fragment are kept in memory with hard references. So if you are keeping a ridiculous amount of Fragments in back stack then you will get out of memory.

Have a look here: When a Fragment is replaced and put in the back stack (or removed) does it stay in memory?

UPDATE: I see your DIALOG_TAG is not changing so you are keeping only one Fragment in backstack at a time, because you remove old one if it exists. In this case you might not have the issue of low memory.

这篇关于Backstack和内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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