How a dialog fragment set result to other fragment - android

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.

Related

How to pass method from Fragment to DialogFragment? Xamarin/Android

In my Activity I have Fragment (ContactBox) which contains "Send" button. If this button`s action fails, user sees error (DialogFragment). I want to implement "Try Again" button on this error, but I dont know how to pass retry action from first Fragment to second DialogFragment. Do you have any ideas? Retry method is placed in ContactBoxFragment.

Calling onBackPressed from a dialogfragment finishes the activity

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.

create Dialog over another dialog

I want to create a dialog. I create a class and class extend Dialog. I create an object from this class when click on a button from my activity and call show method. If i click on button 2 times or more, second dialog create over first dialog and third dialog create over second dialog and more. when i click on back button , my dialogs remove one by one. How can i solve this problem? Please say a recommend. Thanks
Dialog has a function named isShowing() which returns a boolean to indicate that your Dialog Show status , when your button pressed you can check that if previous Dialog isShowing(), don't instantiate it again .

Calling previous activity method on dialog dismiss

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

How to dismiss Dialog on backKey Pressed!

Friends i have an application with an Activitiy which brings some data from Content Providers and Display it in the textViews and edittext onto the Screen. but before that it prompts me for the username and password in a dialog. i have done all the getting content Providers Stuff in the positiveButton onClick Listener of the Alert Dialog.
It works fine but problem is that if i dont enter username and just press bakc key button it closses down the Dialog Box and the Back Screen is showed without loading the content providers.
Note: I have put my code of alert dialog in the onCreate Function of that activity.
So can u guide me how should i do it that when i press back key on dialog box it also should not display my activity.
Please Help!
your dialog name here.setCancelable(false);
this is working
I would honestly just do:
mDialog.setCancelable(false); //assuming the field mDialog is your Dialog
Then make sure you have both an Okay and Cancel (Positive and Negative) buttons on your Dialog. This way, the back press will do nothing, and you can use the Cancel button to finish your activity as well if desired.
The Dialog interface provides a setCancelable() method which enables precisely this. You call that with a false value and the user won't be able to press the back button to go back to your activity.
You can use method of each activity is : onBackPressed()
http://developer.android.com/reference/android/app/Activity.html
-This is the method which you can override sub method to execute when user enter back key.

Categories

Resources