[android-developers] Re: Help me on AsyncTask

2011-01-14 Thread Jimmy hautelook
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 extendsAsyncTaskString, 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


[android-developers] Re: Help me on AsyncTask

2011-01-04 Thread MrChaz
If it's crashing then take a look at the exception that's logged it'll give 
you a line number.

-- 
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

[android-developers] Re: Help me on AsyncTask

2011-01-04 Thread Bob Kerns
Better yet -- use the debugger!

On Jan 4, 6:48 am, MrChaz mrchazmob...@gmail.com wrote:
 If it's crashing then take a look at the exception that's logged it'll give
 you a line number.

-- 
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


[android-developers] Re: Help me on AsyncTask

2011-01-04 Thread JonFHancock
I'm guessing it is in your logic.

If the user is viewing a different Activity, then dialog.isShowing()
is false, so when the ASyncTask finishes, it sees that the dialog is
not showing, so doesn't close it.  When the user switches back to the
calling Activity, then they find the dialog still open with no hope of
ever being closed.

I would either close the dialog before opening the new Intent, or
check if it is open on resume, and if so close it.

On Jan 4, 2:18 am, Mystique joven.ch...@gmail.com wrote:
 Hi,

 I am doing some AsyncTask thing 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 extends AsyncTaskString, 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