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

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.

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.

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 dialog rotation issue.

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

Common Practice when popup a alert dialog in my activity

In my activity, I have an instance variable of AlertDialog aDialog;
under some condition, my activity will pop up a dialog.
My question is if I create aDialog, do I need to dismiss() in my onDestory() or onPause() of my activity?
From my experience, no. If you've called aDialog.show() and it's still showing while the activity is ending or pausing, it may be good to call dismiss(). I don't remember too clearly, but I have had experiences where forgetting to dismiss/close a dialog when switching activities caused a slight undesired, but not problematic, flickering of the dialog on the next activity as it tries to force close the dialog. Hope this answers your question!

Android: When to use Dialog vs. Activity

When is it preferable to use a Dialog box as opposed to an Activity in Android? From my understanding, anything you can do with a Dialog box you can also accomplish with an Activity. Are there any tasks that can only be accomplished by an Activity or a Dialog?
Is what you're doing worth a new Activity? Do you need to be able to start it through an intent? Do you really need to create a new Java class for it?
If it's a straightforward dialog that displays a text and has simple hooks for positive/negative/dismissal functions, definitely use a dialog.
If you have something complex, you may want to go for a full-blown activity.
Well why exactly would you want to start a new activity just to ask the user "Are you sure? Y/N"? Dialogs generally run on top of the activity, and are usually smaller activities or notifications for the user. They also usually have something to do with the process of the app running. It helps make things simpler to open a dialog to prompt the user on top of your activity, than to start a new activity atop your current activity.
I went for Activities when I needed an user interaction that needs backstack, navigation, lifecycle and callable features.. else with dialogs. Being from the WebApp world I ask whether I would have needed a new server page or a pop window for an interaction and the decision in Andoird world becomes easier!
If newServer page then mostly Activity
elseIf popUpWindow then dialog
I created my android application in one fragment with nested alert dialog, so far my application still running well in handle those nested dialog, and I think dialog is less consuming memory than an activity

Categories

Resources