Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting context in AsyncTask

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"

like image 704
D4rWiNS Avatar asked Jun 04 '13 14:06

D4rWiNS


People also ask

What are the problems in AsyncTask?

In summary, the three most common issues with AsyncTask are: Memory leaks. Cancellation of background work. Computational cost.

Why did AsyncTask get deprecated?

Official Reason for Deprecation of AsyncTask AsyncTask was intended to enable proper and easy use of the UI thread. However, the most common use case was for integrating into UI, and that would cause Context leaks, missed callbacks, or crashes on configuration changes.

Is AsyncTask deprecated?

AsyncTask is used to perform time talking operations in android, but it's marked as deprecated from android 11.

What happens to AsyncTask if activity is destroyed?

If you start an AsyncTask inside an Activity and you rotate the device, the Activity will be destroyed and a new instance will be created. But the AsyncTask will not die. It will go on living until it completes.


2 Answers

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(..); 
like image 162
Chintan Rathod Avatar answered Oct 05 '22 23:10

Chintan Rathod


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     }   } 
like image 45
Sai Avatar answered Oct 06 '22 00:10

Sai