I have an alert dialog in my activity. When the user presses the home button when the dialog is dismissed, i would like to have it re-open when the activity resumes.
Right now, i am dismissing the alert onPause() event of the activity.
How can i do this?
probably you could have a variable that indicates that the alert dialog is showing.
bool isAlertShown=false;
set it to true when the dialog is shown.
set it to false when the dialog is dismissed.
onResume event of your activity, check if the variable is true, if yes, you should be showing the dialog.
Show dialogs with DialogFragment, it handles state automatically.
if you want to show every time when your activity resumes show it in your activity's onResume()
if you want to show dialog in case dialog was showing before user pressed home declare a variable such as "isDialogShown" and set it true or false. Do not forget to save your flag variable onSavedInstanceState()
I advice to use Dialog Fragments (http://android-developers.blogspot.com.tr/2012/05/using-dialogfragments.html) for these kind of operations.
Related
My application has a button that shows a custom DialogFragment when the user presses a button. The DialogFragment is shown like this:
if(searchDialog == null)
{
searchDialog = new SearchElementDialog();
searchDialog.setOnItemClickListener(searchElementItemClickListener);
}
searchDialog.show (getFragmentManager(), "SearchElement");
When the user is finished dismiss is called in the usual way and the dialog is removed. Now, when the user presses the same button again I want to show the same dialog, in the same visible state as when the user left it, calling the code in the first block above does display the dialog and its visual state is restored, but:
The screen is not dimmed, and pressing outside of the dialog does not dismiss it and neither are the controls behind it responding.
The dialog does not move when the softkeyboard comes into view.
On the first showing everything is fine. Is it not possible to show the same instance of a dialog again?
What if you hide and show the dialog instead of the whole fragment.
When you want to dismiss call:
searchDialog.getDialog().dismiss();
and when you want to show it:
searchDialog.getDialog().show();
Simas was partly right. getDialog() doesn't return null when the Dialog is shown but it does when there is no Dialog displayed. I stored it as a field in my class
mDialog = getDialog()
and subsequently called
mDialog.show()
which worked every time.
Simply my problem is that i have the following scenario:
I have activity A ... and there is a custom dialog appearing on it .. it's quite big but it's not an activity it's just a dialog ..
Clicking on a certain item on that dialog opens new activity B
After clicking on the dialog .. I need to dismiss the dialog and open Activity B
what happens is that Activity A appears for like a second or less then Activity B opens ..
I've tried dismissing the dialog after calling startActivity() .. but still the A activity still shows briefly before opening activity B ..
Any suggestions ?
As alternative to #user543 solution (which also valid) you can hold on with dismissal of the dialog. Launch new activity from dialog and leave dialog alone (or better maintain a flag for later dialog dismiss), activity will appear immediately on top of Activity A and it's dialog. When user will click back button, onStart and onResume of Activity A will be called, so check if that flag and dismiss the dialog there
Dismiss the dialog in onStop(). Because after loading Activity B only onStop() will call in Activity A.
Define some boolean variable like:
boolean isClicked=false;
Whenever u r performing some click functionaly in dialog, then make this variable as 'true'. Then in onStop() check like this:
#Override
protected void onStop() {
super.onStop();
if(isClicked)
{
//dismiss dialog
}
}
On click of a certain button in class MainActivity, a dialog is shown of some fragment FragDialog:
FragDialog cf = new FragDialog().newInstace();
cf.show(getSupportFragmentManager(), "dialog");
When a hardware back button is pressed OR when an activity MainActivity is focussed, the dialog is dismissed and I returned to the activity MainActivity.
The methods onResume(), onAttach() of activity MainActivity doesn't get called after dialog dismiss.
The idea is to refresh the activity MainActivity after the dialog dismiss in order to get the changed view according to fields selected in the dialog fragment FragDialog.
As mentioned in the doc DialogFragment,
// DialogFragment.show() will take care of adding the fragment
// in a transaction. We also want to remove any currently showing
// dialog, so make our own transaction and take care of that here.
But adding remove() seems pointless as it doesn't getting called, even dialog is dismissing without it.
I wondered that if this will get called, then I can start the activity MainActivity again to reflect the changes.
while you call dialog you are not loosing contrxt of activity, it is not going in inactive state. All you need just make an event to activity when your dialog is closed, it is quite easy if you cancel it by button. Then you just munnually change your View.
Here is nice article about Dialogs
http://developer.android.com/guide/topics/ui/dialogs.html
Currently I am working on a application, which contains one switch. When we touch switch , it initiates a Dialog.
I want to know:
Since My Dialog box is above Activity, when we exit from dialog why onResume us not called.
Which functions get called when Dialog ends
You will need to implement the onCancelled() and onDismiss() listeners for the Dialog. These will be called when the user either cancels, or dismisses the dialog.
The reason onResume() is never called, is because the activity is never paused. Its just an overlay, and your activity carries on in the background.
I have a customDialog with input fields. I want possible entries to be removed once the dialog is closed (either via back or when a certain button is pressed), i.e. the state should not be saved.
How can I do that?
If the back button is pressed means that dialog is canceled. Implement DialogInterface.OnCancelListener for your dialog and empty/delete/null the entries you want.
I think you might be getting another problem.
Say that you have shown a dialog which was dismissed. If the same dialog is going to be shown a second time, it won't be rebuilt. It will just be shown again.
This means that if you setup your dialog in the onCreateDialog method, the second time that the dialog is shown, this method isn't invoked! Instead, onPrepareDialog is invoked.
Alternatives? You can call Activity.removeDialog or take care of the setup process in the onPrepareDialog hook.