Friday, March 11, 2011

Re: [android-developers] Re: ProgressDialog persisting after screen rotation inspite of call to dismissDialog

There is a very simple approach to handle this scenario. Whenever activity is created it calls the life cycle function in following way.

onCreate -> onStart -> onResume

So when you rotate the screen, Android calls the following life cycle function in an order.
onPuase -> onStop -> onDestroy -> onCreate -> onStart -> onCreateDialog -> onPrepareDialog -> onResume

So you just need to have a static flag which needs to be set when onPrepareDialog gets invoked. And then in onResume you need to check this flag if its set then call dismissDialog method to dismiss the dialog.

And moreover you should not call dismissDialog blindly in onPause because onPause is not only called when device is rotated but its also gets invoked in some other scenarios like when you go to another activity. So therefore in those cases it will cause the app to crash.

Thanks
Dalvinder Singh



On Fri, Mar 11, 2011 at 12:38 AM, Kostya Vasilyev <kmansoft@gmail.com> wrote:
Managed dialogs are not saved in the base class's onPause or onSaveInstanceState. That's why I wrote "around the time of", not "in" onSaveInstanceState.

What this means - a managed dialog is a managed dialog, and you can't have a "semi" managed dialog where you first use showDialog() and then decide to not persist it.

That's the point of using a managed dialog - so it's saved and restored for you. If you don't want this behavior, don't use a managed dialog.

( As a side note, managed dialogs can also be reused for efficiency, but that not relevant to your question ).

-- Kostya

10.03.2011 21:56, Shri пишет:

I had taken a look at onSaveInstanceState, but it is called before the
activity is destroyed. Note that I also tried to call dismissDialog in
onPause *before* the call to super.onPause in case the default
implementation of onPause was saving the state of managed dialogs.  I
can certainly switch over to using unmanaged dialogs, but I would be
curious to know which callback is called before managed dialog state
is stored.

Thanks for the reply!

On Mar 9, 9:38 pm, Kostya Vasilyev<kmans...@gmail.com>  wrote:
Removing the dialog in onPause is too late, because by that time the
managed dialogs have been alerady saved (around the time of
onSaveInstanceState callback).

If you don't want the dialog to persist through orientation changes,
don't use a managed dialog.

Create one without calling showDialog, save the reference as an instance
variable of your activity, and dismiss it in onPause.

-- Kostya

10.03.2011 3:57,Shriпишет:



I am seeing that aProgressDialogis persisting after a screen
rotation, even though I am calling dismissDialog in the onPause
method. I do know that a new Activity is created after the screen
rotation and I have read threads like
http://groups.google.com/group/android-developers/browse_thread/threa....
However, my scenario is simpler in that there is no background task
which holds onto the old Activity. Any ideas?
Thanks
Shri
package com.shri.helloandroid;
import android.app.Activity;
import android.os.Bundle;
import android.app.ProgressDialog;
import android.app.Dialog;
import android.view.View;
public class MainActivity extends Activity {
   private static final int DIALOG_LOADING_DOCUMENT = 0;
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
     }
   @Override
   public void onPause() {
     // dismissDialog(DIALOG_LOADING_DOCUMENT);
     super.onPause();
     dismissDialog(DIALOG_LOADING_DOCUMENT);
   }
   //
   // Click the button to start theProgressDialog, and then rotate the
screen. I expect the dialog to be
   // dismissed as onPause gets called when the current Activity is
being destroyed, and it calls dismissDialog.
   // However, I see that the dialog persists after the screen
rotation.
   //
   public void onClick(View view) {
     showDialog(DIALOG_LOADING_DOCUMENT);
   }
   @Override
   protected Dialog onCreateDialog(int id) {
     switch (id) {
       case DIALOG_LOADING_DOCUMENT: {
         ProgressDialogprogressDialog= newProgressDialog(this);
         progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
         progressDialog.setMessage("Loading document...");
         returnprogressDialog;
       }
       default:
         return super.onCreateDialog(id);
     }
   }
}
--
Kostya Vasilyev --http://kmansoft.wordpress.com


--
Kostya Vasilyev -- http://kmansoft.wordpress.com

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

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

No comments:

Post a Comment