Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pass data from activity to the dialogFragment that activity invoked?

In my application I have a form the user fills in. Pressing "save" the data will be saved to the local database. I want to add a confirm dialog for the user to review the details he entered before moving on, since those details are crucial.

In my dialogFragment instance, I would have something like: "You are entering these details: A,B,C... do you confirm?"

A,B,C are the values of my EditText fields in the activity which calls the dialogFragment

How can I access those values from the dialogFragment? I am using:

new ConfirmSaveProjectDetails().show(getFragmentManager(),"Confirm");

in my activity. ConfirmSaveProjectDetails is my dialogFragment class.

I am not using an Intent, otherwise I would send a Bundle...

Any suggestion?

like image 529
Mirko Avatar asked Oct 11 '12 13:10

Mirko


People also ask

What is the difference between dialog & DialogFragment?

Dialog: A dialog is a small window that prompts the user to make a decision or enter additional information. DialogFragment: A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs.

Is DialogFragment deprecated?

This method is deprecated. androidx.

What is DialogFragment used for?

DialogFragment is a utility class which extends the Fragment class. It is a part of the v4 support library and is used to display an overlay modal window within an activity that floats on top of the rest of the content. Essentially a DialogFragment displays a Dialog but inside a Fragment.

How do you show DialogFragment?

Showing the DialogFragment It is not necessary to manually create a FragmentTransaction to display your DialogFragment . Instead, use the show() method to display your dialog. You can pass a reference to a FragmentManager and a String to use as a FragmentTransaction tag.


2 Answers

You can add arguments in the form of a bundle into the Fragment and then retrieve them from the fragment. Use the following methods available on a Fragment:

setArguments and getArguments.

Passing them as arguments to the Fragments constructor is always an option too.

like image 66
PJL Avatar answered Oct 23 '22 02:10

PJL


Perhaps not the most pretty/elegant method, but you could make those bits of information public static and then reference them from the dialog.

In the activity/fragment where you gather the data:

public static String A
public static String B
public static String C

And grab it in the dialog fragment like so (apologies if I'm explaining something you already know):

your_activity/fragment_classname.A
your_activity/fragment_classname.B
your_activity/fragment_classname.C
like image 38
Barak Avatar answered Oct 23 '22 02:10

Barak