0

I have two class, one show images and the next only load images in background, but the problem is that the second class need to do setProgressBarIndeterminateVisibility(false) from the first class, and I dont know ho to do it.

My first class:

public class Galeria extends Activity {

requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

    setContentView(R.layout.activity_galeria);

    setProgressBarIndeterminateVisibility(true); 
    ...
}

My Second class:

public class CargarImagenes extends AsyncTask<Object, Bitmap, Object> {

protected Object doInBackground(Object... params) {

other operations...
}

protected void onPostExecute(Object result) {
    Galeria galeria = new Galeria();


    // This is the problem!!
    galeria.setProgressBarIndeterminateVisibility(false);
}
Anirudha Gupta
  • 9,073
  • 9
  • 54
  • 79
sergio
  • 243
  • 1
  • 2
  • 11

4 Answers4

1

from Galeria

new CargarImagenes().execute(this);

at CargarImagenes

public class CargarImagenes extends AsyncTask<Object, Bitmap, Object> {

    Context context;

    protected Object doInBackground(Object... params) {
        context = (Context) params[0];
    other operations...
    }

    protected void onPostExecute(Object result) {
        Galeria galeria = new Galeria();


        ((Activity)context).setProgressBarIndeterminateVisibility(false)
    }

}   
Georgy Gobozov
  • 13,633
  • 8
  • 72
  • 78
0

When you put Galeria galeria = new Galeria(); in your seccond class, you are making a new instance of Galeria. You need to get the Galeria you created in the first class.

Try doing it like this: (change public to private) and insert the code in your first class.

private class CargarImagenes extends AsyncTask<Object, Bitmap, Object> {

protected Object doInBackground(Object... params) {

other operations...
}

protected void onPostExecute(Object result) {
    //Galeria galeria = new Galeria();


// This is the problem!!
pb.setProgressBarIndeterminateVisibility(false);

}

Where pb is your ProgressBar.

Put it in the first class like public ProgressBar pb;

and do something like
pb= new ProgressBar(this); ....

JanBo
  • 2,925
  • 3
  • 23
  • 32
0

You can user bus classes or libraries, like Otto form Square, or Bus from Guava. They can communicate each other every registered classes. Read more about it on their web pages.

0

It would be better if you can use WeakReference to refer an object from an AsyncTask, because that is a robust way to prevent you accessing a null object.

public class CargarImagenes extends AsyncTask<Void, Bitmap, Object> {

    WeakReference<Activity> mActivity;

    public CargarImagenes(Activity activity){

         mActivity = new WeakReference<Activity>(activity);
    }

    protected Object doInBackground(Void... params) {

        other operations...
    }

    protected void onPostExecute(Object result) {

        Activity activity = mActivity.get();
        if(activity != null){    
            activity.setProgressBarIndeterminateVisibility(false);
        }
    }    
}

then you can create the CargarImagenes like this

CargarImagenes task = new CargarImagenes(this);

and execute it.

Zephyr
  • 6,123
  • 34
  • 33