Android dialog rotation issue. - android

I have a Dialog which depends on information from the current view. This works fine until the unit is rotated at which point the dialog tries to display before the view is built. The dialog is non essential and I'm happy to scrap it on rotation rather than try to replicate the information and hold it for the dialog to be reconstructed.
I've tried calling removeDialog(PHOTO_CREDIT_DIALOG) from the activities onPause method but it doesn't seem to do anything. I've also held a reference to the dialog and tried calling dismiss() in onPause. Also to no avail. The same call when run from a button on the dialog does infact remove it.
thanks,
m

Does onPause get called ? What if you try to remove the Dialog in onConfigurationChanged instead?
http://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange

Related

How to show a yes/no dialog in AsyncTask onPostExecute()

I need to show a yes/no dialog in AsyncTask.onPostExecute() but I keep getting
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
when the screen is rotated just before the dialog is shown. I had a similar problem showing dialogs in the onActivityResult but have since moved the dialogs to the
onPostResume() as suggested by other posts but I am unable to solve this one.
Is it possible to show a yes/no dialog in the onpostexecute() without causing exception and without using "commitAllowingStateLoss"?
Your help is much appreciated.
I think you should maintain state of your activity in which you are calling the dialog, because every time when activities orientation changes onCreate() function calls and if you are initiating any AsyncTask in it, then it will execute it again. May be that's why you are facing the problem. Just maintain its state and then check it, hope it will help you.
Just add this code in your manifest file for that activity. Just type landscape or portrait according to your need.
android:screenOrientation="portrait/landscape"
Thank you.

Android AsyncTask return dismissing a ProgressDialog after screen rotation

Here's the scenario:
Account login page
Clicking on "Sign-in" triggers a login AsyncTask
To block the UI during network access, a ProgressDialog pops up
Upon returning, the ProgressDialog is dismissed and the user forwarded on
This flow works very well.
Here's the problem:
The user may rotate the screen while the AsyncTask is logging him/her in
Presently, the ProgressDialog is referenced by a class field, and dismissed using that pointer and call to .dismiss().
If the screen is rotated, though, everything crashes.
Probably because the Activity is re-created? My suspicion is that the closure around that field reference points to an object that is unreachable. What's your take?
How can I solve it reliably and elegantly? Just add if (... != null) checks?
More generally, I must confess I don't understand the "best practice" to apply in cases like this:
Activity A triggers an AsyncTask
The user navigates away from Activity A (back button? rotate screen? onClick that starts an Intent?)
The AsyncTask returns when Activity A is not the topmost one anymore yet its onPostExecute() has a UI effect , note: the original delegate observer is not available anymore.
Confused * (note: I am a beginner, so a thorough explanation would help me a lot)
Yes on changing the orientation, the activity is destroyed then recreated it again.
When a configuration change occurs at runtime, the activity is shut down and restarted by default, but declaring a configuration with this attribute will prevent the activity from being restarted. Instead, the activity remains running and its onConfigurationChanged() method is called.
Add this line android:configChanges="orientation|keyboardHidden" to your manifest file
<activity
android:name=""
android:label=""
android:configChanges="orientation|keyboardHidden" />
I recommend looking at Handling Runtime Changes. For a detailed explanation of the details of the methods available to you.
android:configChanges="orientation..." tells android your application will take care of resizing the current view hierarchy. As such, when you specify that in your manifest, your activity will not be destroyed and recreated, instead the system will just call your activity's `onConfigurationChanged()` method. As it so happens, most of the stock widgets will resize themselves when their container changes, so if you are using basic layouts, this usually "just works" by redrawing the view hierarchy in the new format. For custom widgets, this trick may not work.
The approved method is to save some configuration instance information when you are being destroyed in the onSaveInstanceState() method, and then recreate your state in onCreate()
In your case, the dialog is dismissed when then screen changes orientation, so you can either leave it that way, or reopen it in your onCreate().

How to cancel or dismiss a custom dialog in its onCreate method?

I have created a custom dialog called MyCustomDialog which extends Dialog. I create and show my custom dialog as follows:
new MyCustomDialog(myContext).show();
I override the Dialog.onCreate(Bundle savedInstanceState) method to do my initialisation. I also check in this method whether a certain condition holds and, if not, I would like to dismiss/cancel my dialog. I have tried calling the cancel() and dismiss() methods in my dialog's onCreate(Bundle savedInstanceState) and onStart() methods but it has no effect.
Anyone know how to cancel or dismiss a dialog (from within the dialog) before it shows?
You should place the logic to determine if the dialog is to be shown outside of the onCreate() method. it does not belong there.
Alternatively, rename your show() method showIfRequired() (or something), and add the conditional show logic there.
I know this doesn't technically answer your question, but what you are trying to do is not the correct design. That's a good thing, as doing in the right way is actually simpler.
Also, as a side note, you should using DialogFragment in favor of Dialog. it's available in the v4 support library.
This is for API levels 10 and below:
First you should override onCreateDialog(int id, Bundle args) in the Activity class, is that what you're doing? Dialogs are always created and displayed as part of the Activity. Second, I don't think you can cancel/dismiss a dialog in onCreateDialog because it hasn't actually been created when onCreateDialog is called. That is, you can't cancel/dismiss something that hasn't been created. What you can try is to override onPrepareDialog() instead and do your check to cancel/dismiss the dialog there. At that point the dialog should actually have been created (just not displayed), so you would be able to prevent it from getting displayed if you call cancel/dismiss there.
onPrepareDialog() is the proper place to do any sort of checks and decision making on the dialog that is about to be displayed. This is for APIs prior to Honeycomb.
This is for APIs 11 and later:
If you are using a later API, you should extend DialogFragment instead. In this case I think you can handle the decision making in onCreateView() method of DialogFragment which is similar to onPrepareDialog().
I hope you've read through this:
http://developer.android.com/guide/topics/ui/dialogs.html
or this, depending on your API:
http://developer.android.com/reference/android/app/DialogFragment.html
Overall, perhaps a cleaner solution is to disable the button or mechanism that causes the dialog to show up in the first place? That is, write you code such that Dialog.show() is called only when it really needs to be called. I'd have to know more details about what exactly you're trying to do. For example, say you call Dialog.show() from the onClickListener of a button. you don't really want the user to press a button, expect a dialog, but have it not show up due to some reason the user doesn't understand. A better solution would be to disable the button all together so that it's obvious to the user that this function isn't available due to something else in the application.

Android: The AlertDialog is invisible when the Activity back to foreground

This question is related to The AlertDialog is invisible when the Activity back to foreground post.
I have the same problem. The previous post is old, and have no answer. Any suggestions how to solve that problem ? Thanks...
For some reason, Dialogs' states must be handled by the developer.
Simply keep a reference to the dialog showing
For example
Dialog showingDialog=null;
Now in onResume()
if(showingdialog!=null)
//show the dialog and maybe resume some state
Have you tried to re-display the AlertDialog when in the activities onResume(). Using Google developers example you would be able to create an instance of this dialog and just recall it.
http://developer.android.com/guide/topics/ui/dialogs.html#AlertDialog
Hope that helps.
Also we all create Dialogs on the fly whenever we need them, we should not.
Android way is (by the book) to override an onCreateDialog(int) and showDialog(int) in our activities so that our dialogs can be managed by the activity lifecycle.
Another way to do so is to use myDialog.setOwnerActivity(MyActivity.this) to tell the dialog it is managed by the activity.

Loading Dialog while rotating device

I have an activity with in which there is a async task that will do some download stuff. AT the time of downlaoding it will show a loading dialog.
My problem is, it worked fine for me when me doing it in only one orentiaon. But when i rotate at the time of download, it shows window leaked and will crash at the
dialog.cancel in my post excute.
From my study on it more i understood it due the change in the context when device is rotated.
That is when a device is rotated the activity will be recreated so the context will be changed.
But i have created the dialog with old one and that wasn't the current context. So when i cancel it it shows error
What is the solution for this, any idea frnds.
Me using honeycomb, me tried but with fragment but didnt get a good sample for that. Me now mainly trying that,
if anyone can give me some links for that it will be
great
First of all: open your dialog using the showDialog method (there are a lot of examples in the official documentation). If you do so, the activity will take care of dismissing the dialog on destroy, and re-showing it after the activity has been recreated.
Also... if the dialog shows a progress bar (not a wheel), you will want to update the progress of the dialog after orientation changes. In order to do so, I recommend to use the onRetainNonConfigurationInstance to return the current state of the dialog and/or the activity itself. Then, you can use getLastNonConfigurationInstance to recover that state. Google about those two methods if you want to see examples.
Another thing to keep in mind: if you are updating the state of the dialog an/or any other UI element from the AsyncTask, you must be aware that after the activity is recreated, the AsyncTask may be pointing to the wrong UI references. In order to handle this, you can create a proxy class (Proxy design pattern) to detach the AsyncTask progress notifications from the current UI elements.

Categories

Resources