I have a dialog fragment that has a close button on it.
This dialog is called from two different activities. One uses it as a simple fragment, and one as a dialog fragment.
In its onClick method, I call requireActivity.onBackPress() so it will close the dialog.
I don't want to call dismiss, because I have a use case in which I want the calling activity to handle it (show a confirmation dialog).
In the activity that shows it as a dialog fragment, calling the onBackPress closes not only the dialog but also finishes the activity :scary_face
Pressing the actual back button just dismisses it though.
Any thoughts on what am I missing here?
Code from the dialog fragment:
viewBinding.closeBtn.setOnClickListener {
requireActivity().onBackPressed()
}
Code that launches the dialog fragment
SharePatientDialogFragment.newInstance(shareData)
.show(context.supportFragmentManager, "share")
You're calling the onBackPressed() of the activity and not from the DialogFragment.
Hence, your activity is closing, and therefore the dialog is also closing.
To fix it, you have to invoke the onBackPressed() of the dialog fragment to ensure that the dialog fragment is dismissed.
Fragment does not possess an onBackPressed() method but the Dialog class does, as so you need to check if it's a dialog and then invoke the method if available.
Related
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.
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
In my app I have several activities one after the other. After my login screen I have Home screen and after that several screens. Now When user select device home button or power off button I want to display login screen when user again comes to my app and then Home screen. Rest all activity I am finishing it from my base class. Now till here I have done, My problem is when I show a dialog in some other activity and at that instance if user click on home or power button, then i am getting WINDOW LEAKED EXCEPTION.
Like I have TempActivity is displaying a dialog and user clicked home button so StoreActivity and TempActivity will finish but Dialog never got chance to be dismissed. So What would be the best way to deal with this situation.
Is there some better way to dismiss the dialog so that I don't get any exception.
Override onDestroy, there, check whether the dialog is present, if so, dismiss it.
dismiss() in onDestroy() doesn't solve this problem. Try to override activity.finish() like:
#Override
public void finish() {
if(mDialog != null) {
mDialog.dismiss();
}
super.finish();
}
Put the Dialog handle in a member object, then when you finish the top activities, dismiss the dialog first.
You could make this more neat by creating abstract Activity class (which all your activities extends), which dismisses possible dialog when calling finish()
I have a fragment that has a button.
When I click on button the system opens a dialog fragment where the user can choose some info.
In the dialog there is a button "Ok". When the user clicks on ok, the dialog dismisses and we return on the other fragment.
I want to return to the other fragment with the result(the info choosen).
How can I do it?
There are several way to achieve this:
You can save the result from the Dialog in the activity (getActivity()) and get in from the other Fragment
You can let the base Fragment implement a callback interface and call its callback function when pressing OK from the Dialog. You'll need to pass the interface (probably it'll be "this") as an argument when creating the Dialog Fragment.
Hope this helps.