android - Backstack and memory leakage -
i have developed small api dialog fragments based on google support library simple requirements:
- api add (or replace) modal dialog
- api dismiss dialog programmatically or user can dismiss dialog pressing button
does api creates memory leakage adding fragments 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, prone low memory, not memory leak though. stack fragment
kept in memory hard references. if keeping ridiculous amount of fragment
s in stack out of memory.
have here: when fragment replaced , put in stack (or removed) stay in memory?
update: see dialog_tag
not changing keeping 1 fragment
in backstack @ time, because remove old 1 if exists. in case might not have issue of low memory.
Comments
Post a Comment