Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use getApplicationContext or Activity.this in a long running AsyncTask

Tags:

android

I have a long running Async Task that sends some data to my server and then stops. The whole process may involve a few requests and response. I have to read data from database, send it and handle the response and update my database accordingly. I am using content providers to read and update data from database.

Now to use the Content Provider, I have to call the getContentResolver() method on context. So I am wondering whether I have to use getApplicationContext or just pass the Activity.this to my methods.

I saw a few posts like this explaining the difference between the two and in most of them they advice us to not use getApplicationContext if possible. Although I do not want my AsyncTask to lose the context from Activity.this when the Activity is destroyed or when orientation changes. So I am wondering if I can use getApplicationContext in my case or will using Activity.this fit my requirement.

like image 312
achie Avatar asked Feb 03 '12 01:02

achie


People also ask

When would you call getApplicationContext () and why?

You only use getApplicationContext() when you know you need a Context for something that may live longer than any other likely Context you have at your disposal. Scenarios include: Use getApplicationContext() if you need something tied to a Context that itself will have global scope.

What is the difference between this context and getApplicationContext which one to use when?

This generally should only be used if you need a Context whose lifecycle is separate from the current context, that is tied to the lifetime of the process rather than the current component. Actually in general it is better to use getApplicationContext() since it will less likey lead to memory leaks.

What is getApplicationContext () Android studio?

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 the current Activity.


1 Answers

There is a reason to use each of the options.

When you are using the context in order to modify the UI, you should use the Activity context, since in some cases using the application context might cause an exception (as described here and here). Such as in the following case:

TextView senderNameTextView = new TextView(getApplicationContext()); 

When you are using the context in cross-activity usage, you should not bind the Activity context to the action, since then even if the activity is destroyed, it will not be garbage-collected, as it is still referenced from the running task. In those cases, you should use the Application context. See the article in Android Developer's site (written by Romain Guy) for even more details.

If you are only using the context to call getContentResolver, you should use the Application context.

like image 186
MByD Avatar answered Sep 30 '22 14:09

MByD