Android: How to get the current application context in AsyncTask?

I am creating an application with several actions, and I have AsyncTask outside of any activity that starts at the beginning of the life of the application. My question is: how can I get the current application context in the Asynctask class?

thank you for your responses

+6
source share
2 answers

You can pass the entire Activity and use getApplicationContext() inside AsyncTask .

+5
source

Pass the context as a parameter to the AsyncTask constructor and save it there as a member. But be careful what type of context you pass to the constructor.

When a task can run during the Activity lifecycle, you must pass in the application context instead of the Activity context. When a task runs just for the life of an Activity, you can pass an Activity object as a context.

+5
source

Source: https://habr.com/ru/post/914454/


All Articles