While running monkey tool on my app, I am getting android.view.WindowLeaked exception, I referred some stackoverflow threads and found that we need avoid this while creating popups, instead we need to use getApplicationContext() but this is causing BadTokenException
E/AndroidRuntime(5597): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
Again after searching through I found this answer which says
One cause of this error may be trying to display an application
window/dialog through a Context that is not an Activity
So how to solve this?
Usually window leaks because You don’t use showDialog method in
Activitybut only usingDialog.show()method.When dialog is displayed and configuration of Your
Activitychanges (eg. orientation change) dialog has not window to attach, and this results in leak of window.If You use Compatibility library You should use DialogFragment instead of pure
dialog.