1

I seem to be going round in circles. I have some code that even on a Galaxy S3 takes a few seconds to run. Drags data from database. I want to add a progress bar popup (spinning circle) around this to give the user that the app is doing something. I have tried Asyntasks elsewhere in app and work fine but for this type the main UI is not waiting for the Asyntask to finish before moving on and so the new activity that is called does not have all the data it needs and crashes.

Is AsyncTask the best way round this or is there an easier way to Puase the main Activity, show a progress bar and then move on once the long deley has been completed.

Thanks for time

UPDATE

public class UpdateDetailsAsyncTask extends AsyncTask<Void, Void, Boolean> {   

    private Context context;
    private TaskCallback callback;

    private ArrayList<Object> object;

    private ProgressDialog progress; 

    public UpdateDetailsAsyncTask (
            Context pContext, 
            ArrayList<Object> pObject,
            TaskCallback pCallback) {

        context = pContext;
        callback = pCallback;
        object = pObject;

    }

    @Override
    protected void onPreExecute() {

        Log.i("AsyncTask", "onPreExecuted");

        progress = new ProgressDialog(context);
        progress.setMessage(context.getResources().getString(R.string.loading));
        progress.show();

    }

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

        Log.i("Archery", "AsyncTask Excuted");
        Log.i("Archery Scorepad", "Building Continue Round Details");

        // Save Data to Database

        return true;

    }


    protected void onPostExecute(Boolean result) {

        Log.i("AsyncTask", "onPostExuted");

        progress.dismiss();

        callback.startNewActivity();

    }

}

Task is called from main Activity

new UpdateDetailsAsyncTask(this, ArrayListOfObjects, this).exute();

UPDATE 2

..

UPDATE 3

The Code that does some work calls an a method within a Util Class which in calls a database class. I have log messages showing for all the rows of data I am saving to the database. It starts correctly and runs through it but the onPostExecute() appears to be called before the database method has completed.

Is my issue that I have nested classes within the activity and the task appears to have completed when the class below it has not?

Thanks

James Dudley
  • 915
  • 4
  • 15
  • 35

2 Answers2

2

You must change to the next activity in onPostExecute from Asyntask

mromer
  • 1,867
  • 1
  • 13
  • 18
  • Is there a way of doing this with having the Anystask as a stand alone. I want to reuse it for future. I have tried a call back but it is still starting the next code before the task has completed – James Dudley May 21 '13 at 09:33
  • Yes. You can create a class with your AsyncTask and you add an interface in the constructor. Then in your onPostExecute you can call the interface that it will be implement in your activity – mromer May 21 '13 at 09:44
  • From your activity that implement MyCallBack: `MyTask myTast = new MyTask(this); myTast.execute();` In onPostExecute: `callBack.doSomeInActivity();` – mromer May 21 '13 at 09:49
  • I have done this. I call back using an interface within the onPostExeute but the interface method is called before the inbackgroudtask appears to have completed – James Dudley May 21 '13 at 09:51
  • onPostExeute always is called after doInBackground. Maybe you are doing async task in doInBackground. If all process are sync in doInBackground, onPostExeute always will be called after doInBackground – mromer May 21 '13 at 09:54
  • Do you do something after `new UpdateDetailsAsyncTask(this, ArrayListOfObjects, this).execute();`? – mromer May 21 '13 at 10:16
  • yes but then I made the code get called by the interface method – James Dudley May 21 '13 at 10:33
  • I have done how I am calling the task and all that is in the startNewActivity(); method (from the interface) there is just some start new activty code – James Dudley May 21 '13 at 10:40
  • what is your util class? if it is async, the class must have a listener – mromer May 22 '13 at 06:04
0

Yes!

Here is a simple code of AsuncTask

private class LoadImageAction extends AsyncTask<String, String, String>{

    private Course course;
    private ProgressBar pb;

    public LoadImageAction(Course course, ProgressBar pb){
        this.course = course;
        this.pb = pb;
    }

    @Override
    protected void onPreExecute(){

    }

    @Override
    protected String doInBackground(String... arg0) {
        // TODO Auto-generated method stub

        return null;
    }

    @Override 
    protected void onProgressUpdate(String... string){


    }

    @Override
    protected void onPostExecute(String result){

    }
}

You can run the action by

new LoadImageAction().execute();
Jeff Lee
  • 783
  • 9
  • 17
  • That is basiclly what I have but the Activity that it is being call from does not seem to stop until the onPostExecute() has been called – James Dudley May 21 '13 at 09:49
  • Sorry, but what is the problem? onPostExecute call when doInBackground is done. – Jeff Lee May 21 '13 at 09:52
  • I add some debug messages to the Save Data to Database and I can see it is not completed before the new activity is called and start loading – James Dudley May 21 '13 at 10:15
  • Are you execute the Task in a right way?? Or Is your code which save data to datavbase is correct?? Because I check your updated code. There is no error. – Jeff Lee May 21 '13 at 15:19