Sunday, May 28, 2017

Nice and Toasty

Normal day, as always.

Imagine my surprise to see this page pop up:







Toast leaked?
My innocent little Toast popups, leak the Activity context?

Curious, I dug in, and sure enough








When you call Toast.makeText(Context, String, Int) a TextView object is created using your passed in context. This means that if you pass it a Service, BroadcastReceiver, or Activity context, and the Toast lingers around longer than the Context you give it, you will leak memory.

The easiest work around is to call Context.getApplicationContext() on whatever you pass into the Toast creation method. However, this can be annoying, because you would need to go through each toast call and do this.

To work around this annoyance, PYDroid now ships with a wrapper utility class called Toasty which does this for you and consumes the standard Toast.makeText() API.

Assuming a simple use of Toasts, simply run a find replace over your project, and replace all instances of Toast.makeText with Toasty.makeText, and you should be good to go (assuming you have autoimport set to resolve conflicts automatically).

========================
Follow pyamsoft around the Web for updates and announcements about the newest applications!
Like what I do?

Send me an email at: pyam.soft@gmail.com
Or find me online at: https://pyamsoft.blogspot.com

Follow my FaceBook Page
Follow my Google+ Page
=========================