4

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();
        }
    }
}
}
Nulldevice
  • 3,926
  • 3
  • 31
  • 37
  • How many dialogs are we talking about since your concerned about memory leak? – Warpzit Jul 29 '13 at 17:20
  • 3
    Why won't you just check? Create several dialogs and dismiss them in various ways (back/ok). Then run a heap dump using Eclipse MAT and search the heap for instances of your dialog. – talkol Jul 29 '13 at 21:05

2 Answers2

4

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.

Community
  • 1
  • 1
M-Wajeeh
  • 17,204
  • 10
  • 66
  • 103
  • Where is he removing the old one? `dismiss()`ing it is not the same as removing it! I'm reasonably sure the backstack just grows in this case. – Delyan Aug 01 '13 at 15:48
  • Look at his `showDialogFragment()` method where he calls `ft.remove(prev);`. He removes previous one before adding. – M-Wajeeh Aug 01 '13 at 16:51
  • Oops, sorry, missed it the first time. – Delyan Aug 01 '13 at 17:38
3

I believe it wont leak but to be sure you need to test it. As talkol said, you should use Eclipse MAT to analyse this issue. There is a good guide by vogel here and a good guide on the Android blog here. Create and close a bunch of dialogs and see if it makes a difference.

On a side note why do you use ft.close() instead of prev.dismiss() in your showDialogFragment() method. The correct way of closing dialogs is dismiss().

Warpzit
  • 27,966
  • 19
  • 103
  • 155