[android-developers] Re: AsyncTask and ProgressDialog
Ok great, thanks.
I've adapted my code and added a static flag to my activity, just for
testing purposes..
This is my code now..
package net.gullycorp.gully;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.Toast;
public class GullyActivity extends Activity {
private final static int DIALOG_TASKING = 1;
private static boolean mTaskComplete = false;
ProgressDialog mLoadingDialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if (!mTaskComplete) {
new Task().execute(); // fetch our stuff
}
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_TASKING:
mLoadingDialog = new ProgressDialog(this);
mLoadingDialog.setMessage("Loading stuff..");
mLoadingDialog.setCancelable(true);
return mLoadingDialog;
}
return super.onCreateDialog(id);
}
private final class Task extends AsyncTask<Void, Void, Void> {
protected void onPreExecute() {
showDialog(DIALOG_TASKING);
}
protected Void doInBackground(Void... unused) {
for (int i = 0; i < 4000000; i++) { }; // just to take some time up
mTaskComplete = true;
return null;
}
protected void onPostExecute(Void unused) {
dismissDialog(DIALOG_TASKING);
Toast.makeText(GullyActivity.this, "Finished..",
Toast.LENGTH_SHORT).show();
}
}
}
This code (kinda) works, the only problem is dismissing the dialog if
the activity is recreated. I've tried dismissing it if mTaskComplete
is true but I guess by that time it's lost reference to the
ProgressDialog.
Thanks
On Nov 7, 2:14 pm, Mark Murphy <mmur...@commonsware.com> wrote:
> Lee Jarvis wrote:
>
> > The code works fine, except when I try and change screen orientation,
>
> > the progress dialog doesn't disappear and the task seems to be
> > executed again, I realize this is because the onCreate() method is
> > being called again on orientation change perhaps?
>
> Yes. By default, on an orientation change, activities are destroyed and
> recreated.
>
> > What would be the best solution for this issue?
>
> I don't know about "best". Some options include:
>
> 1. A static flag or something that indicates whether your background
> task is running, so you only fire off the task in onCreate() if that
> flag is false.
>
> 2. Overriding the default orientation-handling code, so your activity is
> not destroyed and recreated:
>
> http://www.androidguys.com/2008/11/11/rotational-forces-part-three/
>
> 3. Finding some other way of handling your background work that does not
> require it to be invoked every time the activity starts up, if that's
> possible.
>
> --
> Mark Murphy (a Commons Guy)http://commonsware.com|http://twitter.com/commonsguy
>
> _Android Programming Tutorials_ Version 1.0 In Print!
--
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+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home