Reputation: 129
I create an application with several activities and i have an AsyncTask outside any activity which is launched at the begining of the application life. My question is how can i get the current Application Context in the Asynctask class?
Thank's for your answers
Upvotes: 6
Views: 8941
Reputation: 23586
You can pass the whole Activity
and the use getApplicationContext()
inside the AsyncTask
.
Upvotes: 5
Reputation: 27455
Pass the context as an parameter to you AsyncTask's constructor and store it there as a member. But take care which context type you pass to the constructor.
When the task might run over the lifetime of an Activity then you should pass an Application context instead of an Activity context. When the task only runs for the lifetime of an Activity you can pass the Activity object as context.
Upvotes: 5