java - Static context saved in Application class and used in a singleton toast builder, does this create a memory leak? -
i've got singleton toast-showing class, keeps track of current toast , cancels when started again:
public enum singletoast { instance; private toast currenttoast; private string currentmessage; public void show(string message, int duration) { if (message.equals(currentmessage)) { currenttoast.cancel(); } currenttoast = toast.maketext(app.getcontext(), message, duration); currenttoast.show(); currentmessage = message; } }
(extended explanation of example can found here: toast issue in android)
the main difference between answer in link , piece of code context, isn't passed parameter. using static context saved in app class, saved @ startup of app:
public class app extends application { private static context context; @override public void oncreate() { super.oncreate(); context = getapplicationcontext(); } public static context getcontext() { return context; } }
i've read simple context solution in several blogs, , helped lot in retreiving context in classes passing context parameters annoying, or impossible.
however, wonder if creating memory leak!
first of all: static context cause memory leak (if yes, how can prevent without having pass context everywhere?)
secondly: problem use context in toast object, , save field within singleton, if context not static?
first no, if save context in application, described above, not cause memory leak.
about second, same first case, first more simple in implementation second.
Comments
Post a Comment