0

I am really new to Android development. In my app, I perform different async task and needed to save the result from those task to the database which requires context. Inspired from this answer I started using context in all my classes as a member variable. This seemed like a good method, but now ALL my background task and other classes which deal with preference have context as a member variable. Code example -

public class Task extends AsyncTask<Void, Void, String> {
    Context context;

    public Task(Context context){
        super();
        this.context = context;
    }
    protected String doInBackground() {
       //myAsyncTask
    ..
    }
    ..
    protected void onPostExecute(String response) { //response from the request
      DbHelper helper = new DbHelper(context);
      //save to db
    }

I have about 3 or more of such tasks running consecutively at times. So whenever I need to do a background task, I first have to initialize the async task with the context and then initialize the DbHelper using that context. I feel like I'm doing it all wrong. This doesn't seem like a good method to me now (may lead to huge memory leaks imo). Plus it feels like duplication and that I can have access to the context in a better way.

Is it recommended to use context like this? Or does it actually have a disadvantage and I'm better off using some other method? Something like

public class MyApplication extends Application {
    private static Context context;

    public void onCreate(){
        super.onCreate();
        MyApplication.context = getApplicationContext();
    }

    public static Context getAppContext() {
        return MyApplication.context;
        //use context as MyApplication.getAppContext();
    }
}

Which one is better to use?

Community
  • 1
  • 1
aandis
  • 4,084
  • 4
  • 29
  • 40

1 Answers1

0

Neither is correct. Do NOT maintain a reference to Context anywhere, it gives memory leaks because then the VM cannot garbage-collect the Context. We also tried several solutions but all failed. There's no other solution than pass the Context whenever you need it (if you look at the Android API you will see that it also works this way, and it's for a reason).

m0skit0
  • 25,268
  • 11
  • 79
  • 127
  • But isn't passing the `context` everywhere same as having a static access to the context(which is what the second method does). – aandis Feb 11 '15 at 15:11
  • No because you're keeping a static reference to it: `private static Context context;`. – m0skit0 Feb 11 '15 at 15:13
  • 1
    The `applicationContext()` anyway lives as long as the app lives. So why is having a static reference to that context bad? – aandis Feb 11 '15 at 15:21
  • No, it lives forever because it is static. The object instance might be freed, but not the class. So even if Android garbage-collects your MyApplication **instance**, `MyApplication.context` still exists, and thus `Context` cannot be garbage-collected because it is still referenced there. – m0skit0 Feb 11 '15 at 15:30
  • http://stackoverflow.com/questions/3346080/android-references-to-a-context-and-memory-leaks – m0skit0 Feb 11 '15 at 15:37