I defined a separate thread by extending the AsyncTask class. In this class, I do some toasts and dialogs in the AsyncTask onPostExecute and onCancelled . Toasts require an application context, so all I need is:
Toast.makeText(getApplicationContext(),"Some String",1);
Dialogs are created using AlertDialog.Builder , which also requires context in its constructor. Do I believe that this context should be an activity context? i.e.
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
where getActivity can be a user-defined class that returns the current activity. If so, what is the best way to handle this situation? Creating a class of type getActivity or passing the current activity context to the AsyncTask constructor?
I think I'm trying to understand the use of Context . I noticed that a memory leak can be a problem (in fact, this is not yet clear), and how using getApplicationContext() is the best approach where possible.
source share