0

I'm trying to implement simple app which shows ProgressBar updating. Following are my main tasks.

  • When app load first time and start ProgressBar uploading using AsyncTask it should display on UI (Done and working properly)

  • While updating ProgressBar, user exit from the app, uploading value should increment caz it is run on the AsyncTask (Done and working properly)

  • Next time app loading, ProgressBar should be start from the current updating progress value if there is still updating and running AsyncTasks (Issue)

Here is part of my code;

public class PUMainActivity extends Activity implements ProgUpAsync.MyThreadListener {

    private ProgressBar progressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pumain_activity);

        progressBar = (ProgressBar) findViewById(R.id.pumainActivity_progressBar);
    }

    @Override
    public void onTextViewChanging(final int progValue) {

        runOnUiThread(new Runnable() {

            @Override
            public void run() {

                progressBar.setProgress(progValue);
            }
        });
    }
}

Because of the listener I've implemented, I can get the current progress even second time when my app loads. But the ProgressBar UI is not updating. No compile/run time errors.

UPDATE with listener interface


Here is the implementation of listener

public class ProgUpAsync extends AsyncTask<Void, Integer, Void> {

    private MyThreadListener mCallback;

    public interface MyThreadListener {
        public void onTextViewChanging(int progValue);
    }

    public ProgUpAsync(Activity paramActivity) {

        try{
            mCallback = (MyThreadListener) paramActivity;
        }catch(ClassCastException e){
            throw new ClassCastException(paramActivity.toString() + " must implement MyThreadListener");
        }
    }

    @Override
    protected Void doInBackground(Void... params) {

        for(int i=0; i<=100; i++){
            if((i%5) == 0){
                publishProgress(i);

                try {
                    Thread.sleep(5000);
                } catch (InterruptedException ie) {
                }
            }
        }

        return null;
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);

        mCallback.onTextViewChanging(values[0]);
    }
}
rene
  • 41,474
  • 78
  • 114
  • 152
AnujAroshA
  • 4,623
  • 8
  • 56
  • 99
  • The problem is your listener is not getting called. Could you share how you implement your listener? To be able to make the listener working, you should do the necessary handling stuff in onResume and onPause methods of your activity. – Arda Yigithan Orhan Jan 15 '14 at 15:01
  • @AnujAroshA can you please put MyThreadListener class ? i want to implement this feature in my project so please will you post it over here ? – Vaishali Sutariya Sep 03 '14 at 09:17

2 Answers2

0

The problem is you can't retrieve reference to your ProgUpAsync after the application is rebooted. Maybe you have to see to translate this in a Service. Which is do to do this kind of update process.

lithos35
  • 250
  • 2
  • 9
  • That make sense. But even after `onDestroy()` method call of the Activity class (which is PUMainActivity) `onTextViewChanging()` method is running and it's getting correct progress value (progValue) – AnujAroshA Jan 16 '14 at 06:04
  • I thinks the method is called because the Activity thread, which is the AsyncTask callback, is referenced in the AsyncTask, so the Activity thread may not be destroyed until the Asynctask one is destroyed too. But you can't retrieve those instance anyway... – lithos35 Jan 16 '14 at 09:50
0

Finally I did it. Here is the code I've added to get the task done.

In ProgUpAsync.java

public void setMainActivity(Activity paramActivity){

    try{
        mCallback = (MyThreadListener) paramActivity;
    }catch(ClassCastException e){
        throw new ClassCastException(paramActivity.toString() + " must implement MyThreadListener");
    }
}

And in PUMainActivity.java

    try{
        if(progUpAsync.getStatus() == AsyncTask.Status.RUNNING){
            if(PuConstants.debugEnable)Log.d(TAG, "# Async is RUNNING");
            progUpAsync.setMainActivity(this);
        }else{
            if(PuConstants.debugEnable)Log.d(TAG, "# Async is NOT running");
            progUpAsync = new ProgUpAsync(this);
            progUpAsync.execute();
        }
    }catch(NullPointerException npe){
        if(PuConstants.debugEnable)Log.w(TAG, "@ Exception caz of progUpAsync : " + npe);
        progUpAsync = new ProgUpAsync(this);
        progUpAsync.execute();
    }
AnujAroshA
  • 4,623
  • 8
  • 56
  • 99