Difference between getContext() , getApplicationContext() , getBaseContext() and “this”

View.getContext(): Returns the context the view is currently running in. Usually the currently active Activity. Activity.getApplicationContext(): Returns the context for the entire application (the process all the Activities are running inside of). Use this instead of the current Activity context if you need a context tied to the lifecycle of the entire application, not just … Read more

Dialog throwing “Unable to add window — token null is not for an application” with getApplication() as context

My Activity is trying to create an AlertDialog which requires a Context as a parameter. This works as expected if I use: However, I am leery of using “this” as a context due to the potential for memory leaks when Activity is destroyed and recreated even during something simple like a screen rotation. From a … Read more

What is ‘Context’ on Android?

Putting it simply: As the name suggests, it’s the context of the current state of the application/object. It lets newly-created objects understand what has been going on. Typically you call it to get information regarding another part of your program (activity and package/application). You can get the context by invoking getApplicationContext(), getContext(), getBaseContext() or this (when in a class that extends … Read more