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

What is the difference between getContext() , getApplicationContext() , getBaseContext() , and “this“? Though this is simple question I am unable to understand the basic difference between them. Please give some easy examples if possible. 10 s 10 View.getContext(): Returns the context the view is currently running in. Usually the currently active Activity. Activity.getApplicationContext(): Returns … 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: AlertDialog.Builder builder = new AlertDialog.Builder(this); 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 … Read more

What’s “tools:context” in Android layout files?

Starting with a recent new version of ADT, I’ve noticed this new attribute on the layout XML files, for example: <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” xmlns:tools=”http://schemas.android.com/tools” android:orientation=”vertical” android:layout_width=”fill_parent” android:layout_height=”fill_parent” tools:context=”.MainActivity” /> What is “tools:context” used for? How does it even know the exact path to the activity that is written there? Does it look at the package of … Read more

Static way to get ‘Context’ in Android?

Is there a way to get the current Context instance inside a static method? I’m looking for that way because I hate saving the ‘Context’ instance each time it changes. 2Best Answer 21 Do this: In the Android Manifest file, declare the following. <application android:name=”com.xyz.MyApplication”> </application> Then write the class: public class MyApplication extends Application … Read more