[android-developers] Re: AsyncTask and ProgressDialog
I have a different approach to solving the problem at hand. In my opinion AsyncTask is an interesting approach but has too many flaws (like cancel() only working if the Thread is interruptible or issues dismissing Dialogs). The added value of AsyncTask on the other hand is pretty small compared to using a simple Runnable/Thread with some added Handler logic (using a Runnable and adding the Handler/Message part is relatively easy). In other words the benefits of using AsyncTask don't outweigh the added complexity and the possible pitfalls, IMHO. Instead of using AsyncTask I decided to use a Service instead, an approach that has many advantages: - a clear separation of UI (Activity) and background operations (Service) - a service can continue to run in the background even if the user switches to another application - multiple activities or even applications can access the service through a client-server interface - the service can survive even crashes or shutdowns by the VM (e.g. to free up memory) if configured correctly (START_STICKY) - neither orientation changes nor pressing the home button will interrupt the service unless the interruption is required in which case a simple bound service can be used (unbinding stops the service) - The implementation is very robust. It will work irrespective of whether the dialog is managed by the Activity (showDialog(int)) or by the application (dialog.show()) and it doesn't matter either if changes of orientation are handled by the Activity or the app itself. - Android keeps track of whether the service is already running or needs to be instantiated and started This allows a scenario where a background task is started by an Activity and will do whatever it’s meant to do regardless of what happens to the Activity. If an activity is destroyed and recreated the activity will just bind the already running service and update the UI accordingly (e.g. showing/updating a progress dialog). In my opinion the perfect user experience. Because there is no additional thread fiddling with the UI/Dialog there’s no problem dismissing a dialog. The best solution is probably to use showDialog(int) when the service starts and dismissDialog(int) in the onStop() method (and not in onCreate()/onDestroy as Mark suggests because the user interface is visible between onStart() and onStop()). Of course all this doesn’t come for free. Implementing Services can be fairly tricky depending on the requirements and a solid implementation that handles multi-threading correctly can be tricky. On the other hand Lee Jarvis’ scenario is a fairly common one and once a Service is implemented it has a high re-usability. Emanuel Moecklin -- 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: AsyncTask and ProgressDialog
This worked great, though it's quite a bit of setup for something that I think ought to be handled by Android. I just wanted to say thanks, this problem was driving me up the wall. -Daniel On Nov 10, 7:22 pm, Lance Nanek wrote: > Here's a quick attempt at rewriting it without a static. Turned out > more complex than I'd like. Pastebin version:http://pastebin.com/m7b8b184 > > Inline version: > public class MyActivity extends Activity { > > private final static String LOG_TAG = MyActivity.class.getSimpleName > (); > > private final static int DIALOG_ID = 1; > > private Task mTask; > > private boolean mShownDialog; > > @Override > protected void onPrepareDialog(int id, Dialog dialog) { > super.onPrepareDialog(id, dialog); > > if ( id == DIALOG_ID ) { > mShownDialog = true; > } > } > > private void onTaskCompleted() { > Log.i(LOG_TAG, "Activity " + this + " has been notified the > task is > complete."); > > //Check added because dismissDialog throws an exception if the > current > //activity hasn't shown it. This Happens if task finishes > early > enough > //before an orientation change that the dialog is already > gone when > //the previous activity bundles up the dialogs to reshow. > if ( mShownDialog ) { > dismissDialog(DIALOG_ID); > Toast.makeText(this, "Finished..", > Toast.LENGTH_LONG).show(); > } > } > > @Override > protected void onCreate(Bundle savedInstanceState) { > super.onCreate(savedInstanceState); > setContentView(R.layout.main); > > Object retained = getLastNonConfigurationInstance(); > if ( retained instanceof Task ) { > Log.i(LOG_TAG, "Reclaiming previous background > task."); > mTask = (Task) retained; > mTask.setActivity(this); > } else { > Log.i(LOG_TAG, "Creating new background task."); > mTask = new Task(this); > mTask.execute(); > } > } > > @Override > public Object onRetainNonConfigurationInstance() { > mTask.setActivity(null); > return mTask; > } > > @Override > protected Dialog onCreateDialog(int id) { > switch (id) { > case DIALOG_ID: > ProgressDialog dialog = new > ProgressDialog(this); > dialog.setMessage("Loading stuff.."); > dialog.setCancelable(true); > return dialog; > } > return super.onCreateDialog(id); > } > > private static class Task extends AsyncTask { > > private MyActivity activity; > > private boolean completed; > > private Task(MyActivity activity) { > this.activity = activity; > } > > @Override > protected void onPreExecute() { > activity.showDialog(DIALOG_ID); > } > > @Override > protected Void doInBackground(Void... unused) { > try { > Log.i(LOG_TAG, "Background thread starting > sleep."); > Thread.sleep(15 * 1000); > } catch (InterruptedException e) { > Log.e(LOG_TAG, "Thread interrupted:", e); > } > Log.i(LOG_TAG, "Background thread finished sleep."); > return null; > } > > @Override > protected void onPostExecute(Void unused) { > completed = true; > notifyActivityTaskCompleted(); > } > > private void notifyActivityTaskCompleted() { > if ( null != activity ) { > activity.onTaskCompleted(); > } > } > > private void setActivity(MyActivity activity) { > this.activity = activity; > if ( completed ) { > notifyActivityTaskCompleted(); > } > } > > } > > } > > On Nov 10, 5:41 pm, Lee Jarvis wrote: > > > > > I've tried that, too. But again to no avail, nothing I do seems to > > work. I've tried using the callback features whilst also implementing > > weak ref
[android-developers] Re: AsyncTask and ProgressDialog
I have a similar problem. The fact that the Activity is destroyed and re-created every time the orientation changes doesn't make sense to me. Mark Murphy's second suggestion (overriding the default orientation- handling code, so your activity is not destroyed and recreated) actually makes the most sense, and part of me thinks that it should be the default behavior. In my personal opinion, the only thing that an activity should need to do when the orientation changes is to re-draw whatever UI components are already being shown. All this extra work of stopping and restarting dialogs, background tasks, etc. seems unnecessary and extremely bug-prone. Has anyone tried this strategy? If so, how easy was it to re-draw the UI? And what other challenges did you face with this solution? -- PJ On Nov 11, 4:16 am, Lee Jarvis wrote: > Ahh! That makes total sense, I was trying to do more or less the same > thing but it got tricky a there were a few flaws, this is great. I > have a much better understanding of things now. > > Thanks > > On 11 Nov, 01:22, Lance Nanek wrote: > > > > > Here's a quick attempt at rewriting it without a static. Turned out > > more complex than I'd like. Pastebin version:http://pastebin.com/m7b8b184 > > > Inline version: > > public class MyActivity extends Activity { > > > private final static String LOG_TAG = MyActivity.class.getSimpleName > > (); > > > private final static int DIALOG_ID = 1; > > > private Task mTask; > > > private boolean mShownDialog; > > > @Override > > protected void onPrepareDialog(int id, Dialog dialog) { > > super.onPrepareDialog(id, dialog); > > > if ( id == DIALOG_ID ) { > > mShownDialog = true; > > } > > } > > > private void onTaskCompleted() { > > Log.i(LOG_TAG, "Activity " + this + " has been notified the > > task is > > complete."); > > > //Check added because dismissDialog throws an exception if > > the > > current > > //activity hasn't shown it. This Happens if task finishes > > early > > enough > > //before an orientation change that the dialog is already > > gone when > > //the previous activity bundles up the dialogs to reshow. > > if ( mShownDialog ) { > > dismissDialog(DIALOG_ID); > > Toast.makeText(this, "Finished..", > > Toast.LENGTH_LONG).show(); > > } > > } > > > @Override > > protected void onCreate(Bundle savedInstanceState) { > > super.onCreate(savedInstanceState); > > setContentView(R.layout.main); > > > Object retained = getLastNonConfigurationInstance(); > > if ( retained instanceof Task ) { > > Log.i(LOG_TAG, "Reclaiming previous background > > task."); > > mTask = (Task) retained; > > mTask.setActivity(this); > > } else { > > Log.i(LOG_TAG, "Creating new background task."); > > mTask = new Task(this); > > mTask.execute(); > > } > > } > > > @Override > > public Object onRetainNonConfigurationInstance() { > > mTask.setActivity(null); > > return mTask; > > } > > > @Override > > protected Dialog onCreateDialog(int id) { > > switch (id) { > > case DIALOG_ID: > > ProgressDialog dialog = new > > ProgressDialog(this); > > dialog.setMessage("Loading stuff.."); > > dialog.setCancelable(true); > > return dialog; > > } > > return super.onCreateDialog(id); > > } > > > private static class Task extends AsyncTask { > > > private MyActivity activity; > > > private boolean completed; > > > private Task(MyActivity activity) { > > this.activity = activity; > > } > > > @Override > > protected void onPreExecute() { > > activity.showDialog(DIALOG_ID); > > } > > > @Override > > protected Void doInBackground(Void... unused) { > > try { > > Log.i(LOG_TAG, "Background thread starting > > sleep."); > > Thread.sleep(15 * 1000); > > } catch (InterruptedException e) { > > Log.e(LOG_TAG, "Thread interrupted:", e); > > } > > Log.i(LOG_TAG,
[android-developers] Re: AsyncTask and ProgressDialog
Ahh! That makes total sense, I was trying to do more or less the same thing but it got tricky a there were a few flaws, this is great. I have a much better understanding of things now. Thanks On 11 Nov, 01:22, Lance Nanek wrote: > Here's a quick attempt at rewriting it without a static. Turned out > more complex than I'd like. Pastebin version:http://pastebin.com/m7b8b184 > > Inline version: > public class MyActivity extends Activity { > > private final static String LOG_TAG = MyActivity.class.getSimpleName > (); > > private final static int DIALOG_ID = 1; > > private Task mTask; > > private boolean mShownDialog; > > @Override > protected void onPrepareDialog(int id, Dialog dialog) { > super.onPrepareDialog(id, dialog); > > if ( id == DIALOG_ID ) { > mShownDialog = true; > } > } > > private void onTaskCompleted() { > Log.i(LOG_TAG, "Activity " + this + " has been notified the > task is > complete."); > > //Check added because dismissDialog throws an exception if the > current > //activity hasn't shown it. This Happens if task finishes > early > enough > //before an orientation change that the dialog is already > gone when > //the previous activity bundles up the dialogs to reshow. > if ( mShownDialog ) { > dismissDialog(DIALOG_ID); > Toast.makeText(this, "Finished..", > Toast.LENGTH_LONG).show(); > } > } > > @Override > protected void onCreate(Bundle savedInstanceState) { > super.onCreate(savedInstanceState); > setContentView(R.layout.main); > > Object retained = getLastNonConfigurationInstance(); > if ( retained instanceof Task ) { > Log.i(LOG_TAG, "Reclaiming previous background > task."); > mTask = (Task) retained; > mTask.setActivity(this); > } else { > Log.i(LOG_TAG, "Creating new background task."); > mTask = new Task(this); > mTask.execute(); > } > } > > @Override > public Object onRetainNonConfigurationInstance() { > mTask.setActivity(null); > return mTask; > } > > @Override > protected Dialog onCreateDialog(int id) { > switch (id) { > case DIALOG_ID: > ProgressDialog dialog = new > ProgressDialog(this); > dialog.setMessage("Loading stuff.."); > dialog.setCancelable(true); > return dialog; > } > return super.onCreateDialog(id); > } > > private static class Task extends AsyncTask { > > private MyActivity activity; > > private boolean completed; > > private Task(MyActivity activity) { > this.activity = activity; > } > > @Override > protected void onPreExecute() { > activity.showDialog(DIALOG_ID); > } > > @Override > protected Void doInBackground(Void... unused) { > try { > Log.i(LOG_TAG, "Background thread starting > sleep."); > Thread.sleep(15 * 1000); > } catch (InterruptedException e) { > Log.e(LOG_TAG, "Thread interrupted:", e); > } > Log.i(LOG_TAG, "Background thread finished sleep."); > return null; > } > > @Override > protected void onPostExecute(Void unused) { > completed = true; > notifyActivityTaskCompleted(); > } > > private void notifyActivityTaskCompleted() { > if ( null != activity ) { > activity.onTaskCompleted(); > } > } > > private void setActivity(MyActivity activity) { > this.activity = activity; > if ( completed ) { > notifyActivityTaskCompleted(); > } > } > > } > > } > > On Nov 10, 5:41 pm,LeeJarvis wrote: > > > > > I've tried that, too. But again to no avail, nothing I do seems to > > work. I've tried using the callback features whilst also implementing > > weak refere
[android-developers] Re: AsyncTask and ProgressDialog
It's indeed a bit complex, but it's a nice way of doing this! :) I have to look at it a bit closer, it looks like your solution will work when having possibly more than one instance of your activity at any given time. Using the static-solution, that i proposed, won't work well in that scenario. Your basically swapping the activity in and out of a background-task. And the back-ground task is kind-a 'static' because you pass it around through the 'retain-configuration-instance' functionality. Starring this thread :) On Nov 10, 8:22 pm, Lance Nanek wrote: > Here's a quick attempt at rewriting it without a static. Turned out > more complex than I'd like. Pastebin version:http://pastebin.com/m7b8b184 > > Inline version: > public class MyActivity extends Activity { > > private final static String LOG_TAG = MyActivity.class.getSimpleName > (); > > private final static int DIALOG_ID = 1; > > private Task mTask; > > private boolean mShownDialog; > > @Override > protected void onPrepareDialog(int id, Dialog dialog) { > super.onPrepareDialog(id, dialog); > > if ( id == DIALOG_ID ) { > mShownDialog = true; > } > } > > private void onTaskCompleted() { > Log.i(LOG_TAG, "Activity " + this + " has been notified the > task is > complete."); > > //Check added because dismissDialog throws an exception if the > current > //activity hasn't shown it. This Happens if task finishes > early > enough > //before an orientation change that the dialog is already > gone when > //the previous activity bundles up the dialogs to reshow. > if ( mShownDialog ) { > dismissDialog(DIALOG_ID); > Toast.makeText(this, "Finished..", > Toast.LENGTH_LONG).show(); > } > } > > @Override > protected void onCreate(Bundle savedInstanceState) { > super.onCreate(savedInstanceState); > setContentView(R.layout.main); > > Object retained = getLastNonConfigurationInstance(); > if ( retained instanceof Task ) { > Log.i(LOG_TAG, "Reclaiming previous background > task."); > mTask = (Task) retained; > mTask.setActivity(this); > } else { > Log.i(LOG_TAG, "Creating new background task."); > mTask = new Task(this); > mTask.execute(); > } > } > > @Override > public Object onRetainNonConfigurationInstance() { > mTask.setActivity(null); > return mTask; > } > > @Override > protected Dialog onCreateDialog(int id) { > switch (id) { > case DIALOG_ID: > ProgressDialog dialog = new > ProgressDialog(this); > dialog.setMessage("Loading stuff.."); > dialog.setCancelable(true); > return dialog; > } > return super.onCreateDialog(id); > } > > private static class Task extends AsyncTask { > > private MyActivity activity; > > private boolean completed; > > private Task(MyActivity activity) { > this.activity = activity; > } > > @Override > protected void onPreExecute() { > activity.showDialog(DIALOG_ID); > } > > @Override > protected Void doInBackground(Void... unused) { > try { > Log.i(LOG_TAG, "Background thread starting > sleep."); > Thread.sleep(15 * 1000); > } catch (InterruptedException e) { > Log.e(LOG_TAG, "Thread interrupted:", e); > } > Log.i(LOG_TAG, "Background thread finished sleep."); > return null; > } > > @Override > protected void onPostExecute(Void unused) { > completed = true; > notifyActivityTaskCompleted(); > } > > private void notifyActivityTaskCompleted() { > if ( null != activity ) { > activity.onTaskCompleted(); > } > } > > private void setActivity(MyActivity activity) { > this.activity = activity; > if ( completed ) { >
[android-developers] Re: AsyncTask and ProgressDialog
Here's a quick attempt at rewriting it without a static. Turned out more complex than I'd like. Pastebin version: http://pastebin.com/m7b8b184 Inline version: public class MyActivity extends Activity { private final static String LOG_TAG = MyActivity.class.getSimpleName (); private final static int DIALOG_ID = 1; private Task mTask; private boolean mShownDialog; @Override protected void onPrepareDialog(int id, Dialog dialog) { super.onPrepareDialog(id, dialog); if ( id == DIALOG_ID ) { mShownDialog = true; } } private void onTaskCompleted() { Log.i(LOG_TAG, "Activity " + this + " has been notified the task is complete."); //Check added because dismissDialog throws an exception if the current //activity hasn't shown it. This Happens if task finishes early enough //before an orientation change that the dialog is already gone when //the previous activity bundles up the dialogs to reshow. if ( mShownDialog ) { dismissDialog(DIALOG_ID); Toast.makeText(this, "Finished..", Toast.LENGTH_LONG).show(); } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Object retained = getLastNonConfigurationInstance(); if ( retained instanceof Task ) { Log.i(LOG_TAG, "Reclaiming previous background task."); mTask = (Task) retained; mTask.setActivity(this); } else { Log.i(LOG_TAG, "Creating new background task."); mTask = new Task(this); mTask.execute(); } } @Override public Object onRetainNonConfigurationInstance() { mTask.setActivity(null); return mTask; } @Override protected Dialog onCreateDialog(int id) { switch (id) { case DIALOG_ID: ProgressDialog dialog = new ProgressDialog(this); dialog.setMessage("Loading stuff.."); dialog.setCancelable(true); return dialog; } return super.onCreateDialog(id); } private static class Task extends AsyncTask { private MyActivity activity; private boolean completed; private Task(MyActivity activity) { this.activity = activity; } @Override protected void onPreExecute() { activity.showDialog(DIALOG_ID); } @Override protected Void doInBackground(Void... unused) { try { Log.i(LOG_TAG, "Background thread starting sleep."); Thread.sleep(15 * 1000); } catch (InterruptedException e) { Log.e(LOG_TAG, "Thread interrupted:", e); } Log.i(LOG_TAG, "Background thread finished sleep."); return null; } @Override protected void onPostExecute(Void unused) { completed = true; notifyActivityTaskCompleted(); } private void notifyActivityTaskCompleted() { if ( null != activity ) { activity.onTaskCompleted(); } } private void setActivity(MyActivity activity) { this.activity = activity; if ( completed ) { notifyActivityTaskCompleted(); } } } } On Nov 10, 5:41 pm, Lee Jarvis wrote: > I've tried that, too. But again to no avail, nothing I do seems to > work. I've tried using the callback features whilst also implementing > weak references which also doesn't work. Once I change orientation, > the progressDialog never disappears, it's bugging me to hell > > On 8 Nov, 17:41, Streets Of Boston wrote: > > > If you do showDialog(dialogId) and dismissDialog(dailogId), Android > > actively manages the showing and hiding of the dialog on configuration > > changes. > > > E.g. if you called showDialog(id) and your rotate your screen, Android > > will make sure that the dialog is shown again when the activity is > > re
[android-developers] Re: AsyncTask and ProgressDialog
I've tried that, too. But again to no avail, nothing I do seems to work. I've tried using the callback features whilst also implementing weak references which also doesn't work. Once I change orientation, the progressDialog never disappears, it's bugging me to hell On 8 Nov, 17:41, Streets Of Boston wrote: > If you do showDialog(dialogId) and dismissDialog(dailogId), Android > actively manages the showing and hiding of the dialog on configuration > changes. > > E.g. if you called showDialog(id) and your rotate your screen, Android > will make sure that the dialog is shown again when the activity is > recreated for the new configuration. To dismiss it, you have to > explicitly call dismissDialog(id). > > However, you must call 'dismissDialog' on the currently active > activity. That's where the trick with 'ACTIVE_INSTANCE' comes in: > ACTIVE_INSTANCE.showDialog(ACTIVE_TASKING) and > ACTIVE_INSTANCE.dismissDialog(DIALOG_TASKING). > > On Nov 8, 4:28 am,LeeJarvis wrote: > > > > > Well, that's the thing. It's not a progress 'bar' that I can just show/ > > hide which would seem a lot easier. I'm just using a ProgressDialog > > that I dismiss in onPostExecute, The code you see above inside of my > > AsyncTask is what I'm using to show/hide the dialog. I use showDialog > > () in onPreExecute and dismissDialog() in onPostExecute, I realize one > > a new activity is created and the old one is killed it'll lose > > reference to that dialog so I'm guessing that's why dismissDialog() > > doesn't work (on in fact the code in onPostExecute() doesn't get > > executed because the phone has change orientation and it's trying to > > dismiss a dialog which doesn't exist. > > > On 8 Nov, 03:52, Streets Of Boston wrote: > > > > Show us your code that deals with showing/hiding the progress bar and > > > show exactly what doesn't work. > > > Maybe we can figure it out :) > > > > On Nov 7, 5:32 pm,LeeJarvis wrote: > > > > > Any other suggestions? I keep trying different things out but nothing > > > > seems to work, I keep seeing common applications with the same > > > > functionality which is making it more frustrating because I know it > > > > should work fine > > > > > On 7 Nov, 17:29,LeeJarvis wrote: > > > > > > Thanks for your reply, it's made me understand things a little better. > > > > > > Unfortunately that still doesn't seem to work.. > > > > > > The Toast popup appears twice, then the ProgressDialog just continues > > > > > to run regardless > > > > > > On Nov 7, 5:14 pm, Streets Of Boston wrote: > > > > > > > Lance is absolutely right. I ran into this problem a few months ago. > > > > > > > Possible solution: > > > > > > Hold a static handle to your currently active instance of your > > > > > > activity (works only if you have at most one active instance of your > > > > > > activity at any given time in your process). > > > > > > > public class MyActivity extends Activity { > > > > > > public static final MyActivity ACTIVE_INSTANCE; > > > > > > private final static int DIALOG_TASKING = 1; > > > > > > > ProgressDialog mLoadingDialog; > > > > > > > @Override > > > > > > public void onCreate(Bundle savedInstanceState) { > > > > > > > ACTIVE_INSTANCE = this; > > > > > > > super.onCreate(savedInstanceState); > > > > > > setContentView(R.layout.main); > > > > > > > new Task().execute(); > > > > > > } > > > > > > > @Override > > > > > > public void onDestroy() { > > > > > > super.onDestroy(); > > > > > > > ACTIVE_INSTANCE = null; > > > > > > } > > > > > > > @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 static class Task extends AsyncTask { > > > > > > > protected void onPreExecute() { > > > > > > ACTIVE_INSTANCE.showDialog(DIALOG_TASKING); > > > > > > } > > > > > > > protected Void doInBackground(Void... unused) { > > > > > > for (int i = 0; i < 400; i++) { }; // > > > > > > just > > > > > > to take some time up > > > > > > return null; > > > > > > } > > > > > > > protected void onPostExecute(Void unused) { > > > > > > ACTIVE_INSTANCE.dismissDialog > > > > > > (DIALOG_TASKING); > > > > > > Toast.makeText(ACTIVE_INSTANCE, > > > > > > "Finished..", > > > > > > Toast.LENGTH_LONG).show(); > > > > > > } > > > > > > } > > > > > > > } > > > > > > > You may need to put nu
[android-developers] Re: AsyncTask and ProgressDialog
If you do showDialog(dialogId) and dismissDialog(dailogId), Android actively manages the showing and hiding of the dialog on configuration changes. E.g. if you called showDialog(id) and your rotate your screen, Android will make sure that the dialog is shown again when the activity is recreated for the new configuration. To dismiss it, you have to explicitly call dismissDialog(id). However, you must call 'dismissDialog' on the currently active activity. That's where the trick with 'ACTIVE_INSTANCE' comes in: ACTIVE_INSTANCE.showDialog(ACTIVE_TASKING) and ACTIVE_INSTANCE.dismissDialog(DIALOG_TASKING). On Nov 8, 4:28 am, Lee Jarvis wrote: > Well, that's the thing. It's not a progress 'bar' that I can just show/ > hide which would seem a lot easier. I'm just using a ProgressDialog > that I dismiss in onPostExecute, The code you see above inside of my > AsyncTask is what I'm using to show/hide the dialog. I use showDialog > () in onPreExecute and dismissDialog() in onPostExecute, I realize one > a new activity is created and the old one is killed it'll lose > reference to that dialog so I'm guessing that's why dismissDialog() > doesn't work (on in fact the code in onPostExecute() doesn't get > executed because the phone has change orientation and it's trying to > dismiss a dialog which doesn't exist. > > On 8 Nov, 03:52, Streets Of Boston wrote: > > > > > Show us your code that deals with showing/hiding the progress bar and > > show exactly what doesn't work. > > Maybe we can figure it out :) > > > On Nov 7, 5:32 pm, Lee Jarvis wrote: > > > > Any other suggestions? I keep trying different things out but nothing > > > seems to work, I keep seeing common applications with the same > > > functionality which is making it more frustrating because I know it > > > should work fine > > > > On 7 Nov, 17:29, Lee Jarvis wrote: > > > > > Thanks for your reply, it's made me understand things a little better. > > > > > Unfortunately that still doesn't seem to work.. > > > > > The Toast popup appears twice, then the ProgressDialog just continues > > > > to run regardless > > > > > On Nov 7, 5:14 pm, Streets Of Boston wrote: > > > > > > Lance is absolutely right. I ran into this problem a few months ago. > > > > > > Possible solution: > > > > > Hold a static handle to your currently active instance of your > > > > > activity (works only if you have at most one active instance of your > > > > > activity at any given time in your process). > > > > > > public class MyActivity extends Activity { > > > > > public static final MyActivity ACTIVE_INSTANCE; > > > > > private final static int DIALOG_TASKING = 1; > > > > > > ProgressDialog mLoadingDialog; > > > > > > @Override > > > > > public void onCreate(Bundle savedInstanceState) { > > > > > > ACTIVE_INSTANCE = this; > > > > > > super.onCreate(savedInstanceState); > > > > > setContentView(R.layout.main); > > > > > > new Task().execute(); > > > > > } > > > > > > @Override > > > > > public void onDestroy() { > > > > > super.onDestroy(); > > > > > > ACTIVE_INSTANCE = null; > > > > > } > > > > > > @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 static class Task extends AsyncTask { > > > > > > protected void onPreExecute() { > > > > > ACTIVE_INSTANCE.showDialog(DIALOG_TASKING); > > > > > } > > > > > > protected Void doInBackground(Void... unused) { > > > > > for (int i = 0; i < 400; i++) { }; // just > > > > > to take some time up > > > > > return null; > > > > > } > > > > > > protected void onPostExecute(Void unused) { > > > > > ACTIVE_INSTANCE.dismissDialog > > > > > (DIALOG_TASKING); > > > > > Toast.makeText(ACTIVE_INSTANCE, "Finished..", > > > > > Toast.LENGTH_LONG).show(); > > > > > } > > > > > } > > > > > > } > > > > > > You may need to put null checks in your code before using > > > > > ACTIVE_INSTANCE. > > > > > But, you get the idea :) > > > > > > On Nov 7, 11:45 am, Lee Jarvis wrote: > > > > > > > Ah ok, that makes sense. Thanks for your reply. I understand what > > > > > > you're saying, but in all honesty after trying another 3 examples > > > > > > I'm > > > > > > still unable to resolve this, could you possibly provide some kind > > > > > > of > > > > > > example for what would work? > > > > > > > Seems if I use a reference to a
[android-developers] Re: AsyncTask and ProgressDialog
Well, that's the thing. It's not a progress 'bar' that I can just show/ hide which would seem a lot easier. I'm just using a ProgressDialog that I dismiss in onPostExecute, The code you see above inside of my AsyncTask is what I'm using to show/hide the dialog. I use showDialog () in onPreExecute and dismissDialog() in onPostExecute, I realize one a new activity is created and the old one is killed it'll lose reference to that dialog so I'm guessing that's why dismissDialog() doesn't work (on in fact the code in onPostExecute() doesn't get executed because the phone has change orientation and it's trying to dismiss a dialog which doesn't exist. On 8 Nov, 03:52, Streets Of Boston wrote: > Show us your code that deals with showing/hiding the progress bar and > show exactly what doesn't work. > Maybe we can figure it out :) > > On Nov 7, 5:32 pm, Lee Jarvis wrote: > > > > > Any other suggestions? I keep trying different things out but nothing > > seems to work, I keep seeing common applications with the same > > functionality which is making it more frustrating because I know it > > should work fine > > > On 7 Nov, 17:29, Lee Jarvis wrote: > > > > Thanks for your reply, it's made me understand things a little better. > > > > Unfortunately that still doesn't seem to work.. > > > > The Toast popup appears twice, then the ProgressDialog just continues > > > to run regardless > > > > On Nov 7, 5:14 pm, Streets Of Boston wrote: > > > > > Lance is absolutely right. I ran into this problem a few months ago. > > > > > Possible solution: > > > > Hold a static handle to your currently active instance of your > > > > activity (works only if you have at most one active instance of your > > > > activity at any given time in your process). > > > > > public class MyActivity extends Activity { > > > > public static final MyActivity ACTIVE_INSTANCE; > > > > private final static int DIALOG_TASKING = 1; > > > > > ProgressDialog mLoadingDialog; > > > > > @Override > > > > public void onCreate(Bundle savedInstanceState) { > > > > > ACTIVE_INSTANCE = this; > > > > > super.onCreate(savedInstanceState); > > > > setContentView(R.layout.main); > > > > > new Task().execute(); > > > > } > > > > > @Override > > > > public void onDestroy() { > > > > super.onDestroy(); > > > > > ACTIVE_INSTANCE = null; > > > > } > > > > > @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 static class Task extends AsyncTask { > > > > > protected void onPreExecute() { > > > > ACTIVE_INSTANCE.showDialog(DIALOG_TASKING); > > > > } > > > > > protected Void doInBackground(Void... unused) { > > > > for (int i = 0; i < 400; i++) { }; // just > > > > to take some time up > > > > return null; > > > > } > > > > > protected void onPostExecute(Void unused) { > > > > ACTIVE_INSTANCE.dismissDialog > > > > (DIALOG_TASKING); > > > > Toast.makeText(ACTIVE_INSTANCE, "Finished..", > > > > Toast.LENGTH_LONG).show(); > > > > } > > > > } > > > > > } > > > > > You may need to put null checks in your code before using > > > > ACTIVE_INSTANCE. > > > > But, you get the idea :) > > > > > On Nov 7, 11:45 am, Lee Jarvis wrote: > > > > > > Ah ok, that makes sense. Thanks for your reply. I understand what > > > > > you're saying, but in all honesty after trying another 3 examples I'm > > > > > still unable to resolve this, could you possibly provide some kind of > > > > > example for what would work? > > > > > > Seems if I use a reference to a ProgressDialog in my activity it'll > > > > > leak, and if I use the showDialog() methods it'll continue forever. > > > > > > Thanks > > > > > > On Nov 7, 4:39 pm, Lance Nanek wrote: > > > > > > > >private final class Task extends AsyncTask { > > > > > > ... > > > > > > >dismissDialog(DIALOG_TASKING); > > > > > > > A non-static inner class like this has a reference to the instance > > > > > > of > > > > > > the class that created it. So that dismissDialog call probably goes > > > > > > to > > > > > > the previous instance of your activity in this case. Not the current > > > > > > one if there has been an orientation change and the activity has > > > > > > been > > > > > > recreated. > > > > > > > >public void onDestroy() { > > > > > > >dismissDialog(DIALOG_TASKING); > > > > > > > This is called too late to matter. Dialogs create
[android-developers] Re: AsyncTask and ProgressDialog
Show us your code that deals with showing/hiding the progress bar and show exactly what doesn't work. Maybe we can figure it out :) On Nov 7, 5:32 pm, Lee Jarvis wrote: > Any other suggestions? I keep trying different things out but nothing > seems to work, I keep seeing common applications with the same > functionality which is making it more frustrating because I know it > should work fine > > On 7 Nov, 17:29, Lee Jarvis wrote: > > > > > Thanks for your reply, it's made me understand things a little better. > > > Unfortunately that still doesn't seem to work.. > > > The Toast popup appears twice, then the ProgressDialog just continues > > to run regardless > > > On Nov 7, 5:14 pm, Streets Of Boston wrote: > > > > Lance is absolutely right. I ran into this problem a few months ago. > > > > Possible solution: > > > Hold a static handle to your currently active instance of your > > > activity (works only if you have at most one active instance of your > > > activity at any given time in your process). > > > > public class MyActivity extends Activity { > > > public static final MyActivity ACTIVE_INSTANCE; > > > private final static int DIALOG_TASKING = 1; > > > > ProgressDialog mLoadingDialog; > > > > @Override > > > public void onCreate(Bundle savedInstanceState) { > > > > ACTIVE_INSTANCE = this; > > > > super.onCreate(savedInstanceState); > > > setContentView(R.layout.main); > > > > new Task().execute(); > > > } > > > > @Override > > > public void onDestroy() { > > > super.onDestroy(); > > > > ACTIVE_INSTANCE = null; > > > } > > > > @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 static class Task extends AsyncTask { > > > > protected void onPreExecute() { > > > ACTIVE_INSTANCE.showDialog(DIALOG_TASKING); > > > } > > > > protected Void doInBackground(Void... unused) { > > > for (int i = 0; i < 400; i++) { }; // just > > > to take some time up > > > return null; > > > } > > > > protected void onPostExecute(Void unused) { > > > ACTIVE_INSTANCE.dismissDialog > > > (DIALOG_TASKING); > > > Toast.makeText(ACTIVE_INSTANCE, "Finished..", > > > Toast.LENGTH_LONG).show(); > > > } > > > } > > > > } > > > > You may need to put null checks in your code before using > > > ACTIVE_INSTANCE. > > > But, you get the idea :) > > > > On Nov 7, 11:45 am, Lee Jarvis wrote: > > > > > Ah ok, that makes sense. Thanks for your reply. I understand what > > > > you're saying, but in all honesty after trying another 3 examples I'm > > > > still unable to resolve this, could you possibly provide some kind of > > > > example for what would work? > > > > > Seems if I use a reference to a ProgressDialog in my activity it'll > > > > leak, and if I use the showDialog() methods it'll continue forever. > > > > > Thanks > > > > > On Nov 7, 4:39 pm, Lance Nanek wrote: > > > > > > >private final class Task extends AsyncTask { > > > > > ... > > > > > >dismissDialog(DIALOG_TASKING); > > > > > > A non-static inner class like this has a reference to the instance of > > > > > the class that created it. So that dismissDialog call probably goes to > > > > > the previous instance of your activity in this case. Not the current > > > > > one if there has been an orientation change and the activity has been > > > > > recreated. > > > > > > >public void onDestroy() { > > > > > >dismissDialog(DIALOG_TASKING); > > > > > > This is called too late to matter. Dialogs created by showDialog are > > > > > managed by the Activity class. It records which dialogs have to be > > > > > reshown when the activity is recreated due to an orientation change. > > > > > It does so right after the call to onSaveInstanceState. That happens > > > > > before onDestroy. > > > > > > On Nov 7, 9:53 am, Lee Jarvis wrote: > > > > > > > I apologise if I'm missing something or just being stupid.. But i've > > > > > > tried the following.. > > > > > > > @Override > > > > > > public void onDestroy() { > > > > > > dismissDialog(DIALOG_TASKING); > > > > > > super.onDestroy(); > > > > > > } > > > > > > > The dialog is only dismissed if I DONT change orientation, otherwise > > > > > > the "finished" toast will show, but the ProgressDialog will never > > > > > > actually disappear. > > > > > > > On Nov 7, 2:41 pm, Mark Murphy wrote: > > > > > > > > Lee Jarvis wrote: > >
[android-developers] Re: AsyncTask and ProgressDialog
Any other suggestions? I keep trying different things out but nothing seems to work, I keep seeing common applications with the same functionality which is making it more frustrating because I know it should work fine On 7 Nov, 17:29, Lee Jarvis wrote: > Thanks for your reply, it's made me understand things a little better. > > Unfortunately that still doesn't seem to work.. > > The Toast popup appears twice, then the ProgressDialog just continues > to run regardless > > On Nov 7, 5:14 pm, Streets Of Boston wrote: > > > > > Lance is absolutely right. I ran into this problem a few months ago. > > > Possible solution: > > Hold a static handle to your currently active instance of your > > activity (works only if you have at most one active instance of your > > activity at any given time in your process). > > > public class MyActivity extends Activity { > > public static final MyActivity ACTIVE_INSTANCE; > > private final static int DIALOG_TASKING = 1; > > > ProgressDialog mLoadingDialog; > > > @Override > > public void onCreate(Bundle savedInstanceState) { > > > ACTIVE_INSTANCE = this; > > > super.onCreate(savedInstanceState); > > setContentView(R.layout.main); > > > new Task().execute(); > > } > > > @Override > > public void onDestroy() { > > super.onDestroy(); > > > ACTIVE_INSTANCE = null; > > } > > > @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 static class Task extends AsyncTask { > > > protected void onPreExecute() { > > ACTIVE_INSTANCE.showDialog(DIALOG_TASKING); > > } > > > protected Void doInBackground(Void... unused) { > > for (int i = 0; i < 400; i++) { }; // just > > to take some time up > > return null; > > } > > > protected void onPostExecute(Void unused) { > > ACTIVE_INSTANCE.dismissDialog > > (DIALOG_TASKING); > > Toast.makeText(ACTIVE_INSTANCE, "Finished..", > > Toast.LENGTH_LONG).show(); > > } > > } > > > } > > > You may need to put null checks in your code before using > > ACTIVE_INSTANCE. > > But, you get the idea :) > > > On Nov 7, 11:45 am, Lee Jarvis wrote: > > > > Ah ok, that makes sense. Thanks for your reply. I understand what > > > you're saying, but in all honesty after trying another 3 examples I'm > > > still unable to resolve this, could you possibly provide some kind of > > > example for what would work? > > > > Seems if I use a reference to a ProgressDialog in my activity it'll > > > leak, and if I use the showDialog() methods it'll continue forever. > > > > Thanks > > > > On Nov 7, 4:39 pm, Lance Nanek wrote: > > > > > >private final class Task extends AsyncTask { > > > > ... > > > > >dismissDialog(DIALOG_TASKING); > > > > > A non-static inner class like this has a reference to the instance of > > > > the class that created it. So that dismissDialog call probably goes to > > > > the previous instance of your activity in this case. Not the current > > > > one if there has been an orientation change and the activity has been > > > > recreated. > > > > > >public void onDestroy() { > > > > >dismissDialog(DIALOG_TASKING); > > > > > This is called too late to matter. Dialogs created by showDialog are > > > > managed by the Activity class. It records which dialogs have to be > > > > reshown when the activity is recreated due to an orientation change. > > > > It does so right after the call to onSaveInstanceState. That happens > > > > before onDestroy. > > > > > On Nov 7, 9:53 am, Lee Jarvis wrote: > > > > > > I apologise if I'm missing something or just being stupid.. But i've > > > > > tried the following.. > > > > > > @Override > > > > > public void onDestroy() { > > > > > dismissDialog(DIALOG_TASKING); > > > > > super.onDestroy(); > > > > > } > > > > > > The dialog is only dismissed if I DONT change orientation, otherwise > > > > > the "finished" toast will show, but the ProgressDialog will never > > > > > actually disappear. > > > > > > On Nov 7, 2:41 pm, Mark Murphy wrote: > > > > > > > Lee Jarvis wrote: > > > > > > > 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. > > > > > > > You may need to dismiss the dialog in onDestroy() and reopen
[android-developers] Re: AsyncTask and ProgressDialog
Thanks for your reply, it's made me understand things a little better. Unfortunately that still doesn't seem to work.. The Toast popup appears twice, then the ProgressDialog just continues to run regardless On Nov 7, 5:14 pm, Streets Of Boston wrote: > Lance is absolutely right. I ran into this problem a few months ago. > > Possible solution: > Hold a static handle to your currently active instance of your > activity (works only if you have at most one active instance of your > activity at any given time in your process). > > public class MyActivity extends Activity { > public static final MyActivity ACTIVE_INSTANCE; > private final static int DIALOG_TASKING = 1; > > ProgressDialog mLoadingDialog; > > @Override > public void onCreate(Bundle savedInstanceState) { > > ACTIVE_INSTANCE = this; > > super.onCreate(savedInstanceState); > setContentView(R.layout.main); > > new Task().execute(); > } > > @Override > public void onDestroy() { > super.onDestroy(); > > ACTIVE_INSTANCE = null; > } > > @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 static class Task extends AsyncTask { > > protected void onPreExecute() { > ACTIVE_INSTANCE.showDialog(DIALOG_TASKING); > } > > protected Void doInBackground(Void... unused) { > for (int i = 0; i < 400; i++) { }; // just > to take some time up > return null; > } > > protected void onPostExecute(Void unused) { > ACTIVE_INSTANCE.dismissDialog > (DIALOG_TASKING); > Toast.makeText(ACTIVE_INSTANCE, "Finished..", > Toast.LENGTH_LONG).show(); > } > } > > } > > You may need to put null checks in your code before using > ACTIVE_INSTANCE. > But, you get the idea :) > > On Nov 7, 11:45 am, Lee Jarvis wrote: > > > > > Ah ok, that makes sense. Thanks for your reply. I understand what > > you're saying, but in all honesty after trying another 3 examples I'm > > still unable to resolve this, could you possibly provide some kind of > > example for what would work? > > > Seems if I use a reference to a ProgressDialog in my activity it'll > > leak, and if I use the showDialog() methods it'll continue forever. > > > Thanks > > > On Nov 7, 4:39 pm, Lance Nanek wrote: > > > > >private final class Task extends AsyncTask { > > > ... > > > >dismissDialog(DIALOG_TASKING); > > > > A non-static inner class like this has a reference to the instance of > > > the class that created it. So that dismissDialog call probably goes to > > > the previous instance of your activity in this case. Not the current > > > one if there has been an orientation change and the activity has been > > > recreated. > > > > >public void onDestroy() { > > > >dismissDialog(DIALOG_TASKING); > > > > This is called too late to matter. Dialogs created by showDialog are > > > managed by the Activity class. It records which dialogs have to be > > > reshown when the activity is recreated due to an orientation change. > > > It does so right after the call to onSaveInstanceState. That happens > > > before onDestroy. > > > > On Nov 7, 9:53 am, Lee Jarvis wrote: > > > > > I apologise if I'm missing something or just being stupid.. But i've > > > > tried the following.. > > > > > @Override > > > > public void onDestroy() { > > > > dismissDialog(DIALOG_TASKING); > > > > super.onDestroy(); > > > > } > > > > > The dialog is only dismissed if I DONT change orientation, otherwise > > > > the "finished" toast will show, but the ProgressDialog will never > > > > actually disappear. > > > > > On Nov 7, 2:41 pm, Mark Murphy wrote: > > > > > > Lee Jarvis wrote: > > > > > > 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. > > > > > > You may need to dismiss the dialog in onDestroy() and reopen it in > > > > > onCreate(), if the flag is true. > > > > > > -- > > > > > Mark Murphy (a Commons > > > > > Guy)http://commonsware.com|http://twitter.com/commonsguy > > > > > > Android App Developer Books:http://commonsware.com/books-Hide quoted > > > > > text - > > > - Show quoted text - -- 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 u
[android-developers] Re: AsyncTask and ProgressDialog
Lance is absolutely right. I ran into this problem a few months ago. Possible solution: Hold a static handle to your currently active instance of your activity (works only if you have at most one active instance of your activity at any given time in your process). public class MyActivity extends Activity { public static final MyActivity ACTIVE_INSTANCE; private final static int DIALOG_TASKING = 1; ProgressDialog mLoadingDialog; @Override public void onCreate(Bundle savedInstanceState) { ACTIVE_INSTANCE = this; super.onCreate(savedInstanceState); setContentView(R.layout.main); new Task().execute(); } @Override public void onDestroy() { super.onDestroy(); ACTIVE_INSTANCE = null; } @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 static class Task extends AsyncTask { protected void onPreExecute() { ACTIVE_INSTANCE.showDialog(DIALOG_TASKING); } protected Void doInBackground(Void... unused) { for (int i = 0; i < 400; i++) { }; // just to take some time up return null; } protected void onPostExecute(Void unused) { ACTIVE_INSTANCE.dismissDialog (DIALOG_TASKING); Toast.makeText(ACTIVE_INSTANCE, "Finished..", Toast.LENGTH_LONG).show(); } } } You may need to put null checks in your code before using ACTIVE_INSTANCE. But, you get the idea :) On Nov 7, 11:45 am, Lee Jarvis wrote: > Ah ok, that makes sense. Thanks for your reply. I understand what > you're saying, but in all honesty after trying another 3 examples I'm > still unable to resolve this, could you possibly provide some kind of > example for what would work? > > Seems if I use a reference to a ProgressDialog in my activity it'll > leak, and if I use the showDialog() methods it'll continue forever. > > Thanks > > On Nov 7, 4:39 pm, Lance Nanek wrote: > > > > > >private final class Task extends AsyncTask { > > ... > > >dismissDialog(DIALOG_TASKING); > > > A non-static inner class like this has a reference to the instance of > > the class that created it. So that dismissDialog call probably goes to > > the previous instance of your activity in this case. Not the current > > one if there has been an orientation change and the activity has been > > recreated. > > > >public void onDestroy() { > > >dismissDialog(DIALOG_TASKING); > > > This is called too late to matter. Dialogs created by showDialog are > > managed by the Activity class. It records which dialogs have to be > > reshown when the activity is recreated due to an orientation change. > > It does so right after the call to onSaveInstanceState. That happens > > before onDestroy. > > > On Nov 7, 9:53 am, Lee Jarvis wrote: > > > > I apologise if I'm missing something or just being stupid.. But i've > > > tried the following.. > > > > @Override > > > public void onDestroy() { > > > dismissDialog(DIALOG_TASKING); > > > super.onDestroy(); > > > } > > > > The dialog is only dismissed if I DONT change orientation, otherwise > > > the "finished" toast will show, but the ProgressDialog will never > > > actually disappear. > > > > On Nov 7, 2:41 pm, Mark Murphy wrote: > > > > > Lee Jarvis wrote: > > > > > 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. > > > > > You may need to dismiss the dialog in onDestroy() and reopen it in > > > > onCreate(), if the flag is true. > > > > > -- > > > > Mark Murphy (a Commons > > > > Guy)http://commonsware.com|http://twitter.com/commonsguy > > > > > Android App Developer Books:http://commonsware.com/books- Hide quoted > > > > text - > > - Show quoted text - -- 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: AsyncTask and ProgressDialog
Ah ok, that makes sense. Thanks for your reply. I understand what you're saying, but in all honesty after trying another 3 examples I'm still unable to resolve this, could you possibly provide some kind of example for what would work? Seems if I use a reference to a ProgressDialog in my activity it'll leak, and if I use the showDialog() methods it'll continue forever. Thanks On Nov 7, 4:39 pm, Lance Nanek wrote: > >private final class Task extends AsyncTask { > ... > >dismissDialog(DIALOG_TASKING); > > A non-static inner class like this has a reference to the instance of > the class that created it. So that dismissDialog call probably goes to > the previous instance of your activity in this case. Not the current > one if there has been an orientation change and the activity has been > recreated. > > >public void onDestroy() { > >dismissDialog(DIALOG_TASKING); > > This is called too late to matter. Dialogs created by showDialog are > managed by the Activity class. It records which dialogs have to be > reshown when the activity is recreated due to an orientation change. > It does so right after the call to onSaveInstanceState. That happens > before onDestroy. > > On Nov 7, 9:53 am, Lee Jarvis wrote: > > > > > I apologise if I'm missing something or just being stupid.. But i've > > tried the following.. > > > @Override > > public void onDestroy() { > > dismissDialog(DIALOG_TASKING); > > super.onDestroy(); > > } > > > The dialog is only dismissed if I DONT change orientation, otherwise > > the "finished" toast will show, but the ProgressDialog will never > > actually disappear. > > > On Nov 7, 2:41 pm, Mark Murphy wrote: > > > > Lee Jarvis wrote: > > > > 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. > > > > You may need to dismiss the dialog in onDestroy() and reopen it in > > > onCreate(), if the flag is true. > > > > -- > > > Mark Murphy (a Commons > > > Guy)http://commonsware.com|http://twitter.com/commonsguy > > > > Android App Developer Books:http://commonsware.com/books -- 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: AsyncTask and ProgressDialog
>private final class Task extends AsyncTask { ... >dismissDialog(DIALOG_TASKING); A non-static inner class like this has a reference to the instance of the class that created it. So that dismissDialog call probably goes to the previous instance of your activity in this case. Not the current one if there has been an orientation change and the activity has been recreated. >public void onDestroy() { >dismissDialog(DIALOG_TASKING); This is called too late to matter. Dialogs created by showDialog are managed by the Activity class. It records which dialogs have to be reshown when the activity is recreated due to an orientation change. It does so right after the call to onSaveInstanceState. That happens before onDestroy. On Nov 7, 9:53 am, Lee Jarvis wrote: > I apologise if I'm missing something or just being stupid.. But i've > tried the following.. > > @Override > public void onDestroy() { > dismissDialog(DIALOG_TASKING); > super.onDestroy(); > } > > The dialog is only dismissed if I DONT change orientation, otherwise > the "finished" toast will show, but the ProgressDialog will never > actually disappear. > > On Nov 7, 2:41 pm, Mark Murphy wrote: > > > Lee Jarvis wrote: > > > 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. > > > You may need to dismiss the dialog in onDestroy() and reopen it in > > onCreate(), if the flag is true. > > > -- > > Mark Murphy (a Commons > > Guy)http://commonsware.com|http://twitter.com/commonsguy > > > Android App Developer Books:http://commonsware.com/books -- 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: AsyncTask and ProgressDialog
Anyone other feedback on this? I'm confused as to why this isn't simpler.. All I want to do is display the ProgressDialog until the task is complete, then get rid of it.. Anyone would think this was trivial. No matter what I do I'm unable to remove the dialog once the screens orientation has altered. Thanks for any replys On 7 Nov, 14:53, Lee Jarvis wrote: > I apologise if I'm missing something or just being stupid.. But i've > tried the following.. > > @Override > public void onDestroy() { > dismissDialog(DIALOG_TASKING); > super.onDestroy(); > } > > The dialog is only dismissed if I DONT change orientation, otherwise > the "finished" toast will show, but the ProgressDialog will never > actually disappear. > > On Nov 7, 2:41 pm, Mark Murphy wrote: > > > > > Lee Jarvis wrote: > > > 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. > > > You may need to dismiss the dialog in onDestroy() and reopen it in > > onCreate(), if the flag is true. > > > -- > > Mark Murphy (a Commons > > Guy)http://commonsware.com|http://twitter.com/commonsguy > > > Android App Developer Books:http://commonsware.com/books -- 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: AsyncTask and ProgressDialog
I apologise if I'm missing something or just being stupid.. But i've tried the following.. @Override public void onDestroy() { dismissDialog(DIALOG_TASKING); super.onDestroy(); } The dialog is only dismissed if I DONT change orientation, otherwise the "finished" toast will show, but the ProgressDialog will never actually disappear. On Nov 7, 2:41 pm, Mark Murphy wrote: > Lee Jarvis wrote: > > 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. > > You may need to dismiss the dialog in onDestroy() and reopen it in > onCreate(), if the flag is true. > > -- > Mark Murphy (a Commons > Guy)http://commonsware.com|http://twitter.com/commonsguy > > Android App Developer Books:http://commonsware.com/books -- 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
Re: [android-developers] Re: AsyncTask and ProgressDialog
Lee Jarvis wrote: > 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. You may need to dismiss the dialog in onDestroy() and reopen it in onCreate(), if the flag is true. -- Mark Murphy (a Commons Guy) http://commonsware.com | http://twitter.com/commonsguy Android App Developer Books: http://commonsware.com/books -- 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: 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 { protected void onPreExecute() { showDialog(DIALOG_TASKING); } protected Void doInBackground(Void... unused) { for (int i = 0; i < 400; 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 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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en