getting context in AsyncTask

AndroidAndroid AsynctaskToastAndroid Context

Android Problem Overview


I am trying to get the context in my AsyncTask of the class called Opciones(this class is the only one that call that task) but I don't know how to do it, I saw some code like this:

      protected void onPostExecute(Long result) {
	
	Toast.makeText(Opciones.this,"Subiendo la foto. ¡Tras ser moderada empezara a ser votada!: ", Toast.LENGTH_LONG).show(); 
}

But it doesn't work for me it says: "No enclosing instance of the type Opciones in scope"

Android Solutions


Solution 1 - Android

You need to do following things.

  • when you want to use AsyncTask, extend that in other class say MyCustomTask.
  • in constructor of new class, pass Context

Example

public class MyCustomTask extends AsyncTask<Void, Void, Long> {
	
    private Context mContext;
	
	public MyCustomTask (Context context){
		 mContext = context;
	}
	
	//other methods like onPreExecute etc.
	protected void onPostExecute(Long result) {
		 Toast.makeText(mContext,"Subiendo la foto. ¡Tras ser moderada empezara a ser votada!: ", Toast.LENGTH_LONG).show(); 
	}
}

And instantiate class by following.

MyCustomTask task = new MyCustomTask(context);
task.execute(..);

Solution 2 - Android

Holding a weak reference to the host Activity will prevent memory leaks.

static class MyTask extends AsyncTask<Void, Void, Void> {
    // Weak references will still allow the Activity to be garbage-collected
    private final WeakReference<Activity> weakActivity;

    MyTask(Activity myActivity) {
      this.weakActivity = new WeakReference<>(myActivity);
    }

    @Override
    public Void doInBackground(Void... params) {
      // do async stuff here
    }

    @Override
    public void onPostExecute(Void result) {
      // Re-acquire a strong reference to the activity, and verify
      // that it still exists and is active.
      Activity activity = weakActivity.get();
      if (activity == null
          || activity.isFinishing()
          || activity.isDestroyed()) {
        // activity is no longer valid, don't do anything!
        return;
      }

      // The activity is still valid, do main-thread stuff here
    }
  }

Solution 3 - Android

Since only one Activity uses this task then just make it an inner class of that Activity

public class Opciones extends Activity
{
     public void onCreate()
     {
         ...
     }

    public class MyTask extends AsyncTask<>
    {
        ...

         protected void onPostExecute(Long result) {
        Toast.makeText(Opciones.this,"Subiendo la foto. ¡Tras ser moderada empezara a ser votada!: ", Toast.LENGTH_LONG).show(); 
     }
}

Then you have access to member variables of the Activity and its Context

Solution 4 - Android

You can write getApplicationContex(). Or Define global variable.

Activity activity;

And at the onCreate() function

activity = this;

then,

 protected void onPostExecute(Long result) {

    Toast.makeText(activity,"Subiendo la foto. ¡Tras ser moderada empezara a ser votada!: ", Toast.LENGTH_LONG).show(); 
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionD4rWiNSView Question on Stackoverflow
Solution 1 - AndroidChintan RathodView Answer on Stackoverflow
Solution 2 - AndroidSaiView Answer on Stackoverflow
Solution 3 - AndroidcodeMagicView Answer on Stackoverflow
Solution 4 - Androidyahya.canView Answer on Stackoverflow