views:

8425

answers:

4

In an Android app, is there anything wrong with the following approach:

public class MyApp extends android.app.Application {

    private static MyApp instance;

    public MyApp() {
     instance = this;
    }

    public static Context getContext() {
     return instance;
    }

}

and pass it everywhere (e.g. SQLiteOpenHelper) where context is required (and not leaking of course)?

A: 

In my experience this approach shouldn't be necessary. If you need the context for anything you can usually get it via a call to View.getContext() and using the Context obtained there you can call Context.getApplicationContext() to get the Application context. If you are trying to get the Appication context this from an Activity you can always call Activity.getApplication() which should be able to be passed as the Context needed for a call to SQLiteOpenHelper()

Overall there doesn't seem to be a problem with your approach for this situation, but when dealing with Context just make sure you are not leaking memory anywhere as described on the official Google Android Developers blog

snctln
The approach you're suggesting is pretty likely to cause the memory leaks the blog post you link to describes. The context returned by the View object will be for the Activity rather than the Application.
Reto Meier
Reto - You are correct, I failed to mention that in order to get the Applciaiton context you need to call getApplciationContext() on the context that you get get from View.getContext() as you said in your answer, I am editing now to include that piece of information. Thank you for pointing it out.
snctln
+8  A: 

There are a couple of potential problems with this approach, though in a lot of circumstances (such as your example) it will work well.

In particular you should be careful when dealing with anything that deals with the GUI that requires a Context. For example, if you pass the application Context into the LayoutInflator you will get an Exception. Generally speaking, you're approach is excellent: it's good practice to use an Activity's Context within that Activity, and the Application Context when passing a context beyond the scope of an Activity to avoid memory leaks.

Also, as an alternative to your pattern you can use the shortcut of calling getApplicationContext() on a Context object (such as an Activity) to get the Application Context.

Reto Meier
Thanks for an inspiring answer. I think I'll use this approach solely for the persistence layer (as I don't want to go with content providers). Wondering what was the motivation behind designing SQLiteOpenHelper in a way that expects a Context to be supplied instead of acquiring it from Application itself.P.S. And your book is great!
alex
A: 

I had a question on this, for me it doesn't look like this will work. If this has worked for you I am very interested; normally in java the static call can be made before the constructor is run, so unless the container is somehow instantiating the class(or some other class is at runtime), the call to getContext() should always return null. And if another class is instantiating it, how can you be sure that this has already run before you try to use this method?

I just wanted to be sure I am understanding this correctly. So in your application in one of your starting activities, you are instantiating this(which then sets the static context variable), correct? This is something that I have been struggling with, since I really don't see where the primary thread starts in an android app, since the various intents and activities can be invoked by other applications at any time(at least this is my understanding).

I am pretty new to android, so if I am not getting it or have some fundamental flaw in how I think this works, please let me know.

scott
alex
A: 

You are trying to create a wrapper to get Application Context and there is a possibility that it might return "null" pointer.

As per my understanding, I guess its better approach to call- any of the 2 Context.getApplicationContext() or Activity.getApplication().

Prasanta