I'm guessing that you've restarted your async task again when you
clicked back.  If your intention was to run it again, make sure you
create a new instance every time.  Each instance of AsyncTask can only
be executed once.  if you executed more than once, you'll get an
exception.  Example:

# this is OK, because you are calling execute() on a new instance
every time

  public class MyActivity extends Activity {

     @Override
     public void onCreate(Bundle savedInstanceState) {
         new LongOperation.execute();
     }
  }



# this is NOT OK, because you are calling multiple execute() on the
same instance.

  public class MyActivity extends Activity {

     LongOperation task = new LongOperation();

     @Override
     public void onCreate(Bundle savedInstanceState) {
         task.execute(); // on click back or orientation change,
exception will occure
     }
  }


if you only want to run it once per activity, i would probably just
set a static variable flag within the the activity to check against
ever time when the AsyncTask is called. something like,


  public class MyActivity extends Activity {

     static Boolean flag=true;

     @Override
     public void onCreate(Bundle savedInstanceState) {
          if(flag) {
               new LongOperation.execute();
               flag= false;
          }
     }
  }

when you call finish() to destroy the activity, the flag will get
wiped out as well.  so the next time you start the same activity,
asynctask will executed again.


now, if you only want to run it once per application startup, you can
use the same strategy but save the static flag in your own
implementation of the application class.





On Jan 4, 2:18 am, Mystique <joven.ch...@gmail.com> wrote:
> Hi,
>
> I am doing someAsyncTaskthing and seems to work ok and after that I
> open another intent to display a listview.
> The thing is, if the user press the back key, it return to the caller
> intent showing the progress dialog and seems to be hanging there until
> it crash. What did I do wrong? I already do a dialog.dismiss()
> onPostExecute().
>
> Please help me out.
>
> Many thanks,
> CJ
>
>     private class LongOperation extendsAsyncTask<String, Void,
> String> {
>         ProgressDialog dialog;
>
>         @Override
>         protected String doInBackground(String... params) {
>                 // download something from the net and processing it.
>                 // work perfectly
>                 return null;
>         }
>
>         @Override
>         protected void onPostExecute(String result) {
>                 // execution of result of Long time consuming operation
>                 // some processing from the information taken from the
> internet (doInBackground)
>                 if (dialog.isShowing()) {
>                 dialog.dismiss();
>             }
>         }
>
>         /* (non-Javadoc)
>          * @see android.os.AsyncTask#onPreExecute()
>          */
>         @Override
>         protected void onPreExecute() {
>         // Things to be done before execution of long running operation.
> For example showing ProgessDialog
>                 dialog = ProgressDialog.show(Main.this, "",
>                     "Receiving data", true);
>         }
>
>         @Override
>         protected void onProgressUpdate(Void... values) {
>           // Things to be done while execution of long running
> operation is in progress. For example updating ProgessDialog
>          }
>     }

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to