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

Mirko picture Mirko · Oct 11, 2012 · Viewed 18.6k times · Source

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?

Answer

PJL picture PJL · Oct 11, 2012

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.