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

你说的曾经没有我的故事 提交于 2019-12-03 12:21:12

问题


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?


回答1:


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.




回答2:


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



回答3:


In your Activity

ImageViewDialogFragment dialogFragment = new ImageViewDialogFragment ();
                        Bundle bundle = new Bundle();
                        bundle.putString("link",moviesList.get(position).getImage());
                        dialogFragment.setArguments(bundle);
                        dialogFragment.show((GalleryReviewActivity.this).getSupportFragmentManager(),"Image Dialog");

in your DialogFragment

 Bundle bundle = getArguments();
 String imageLink = bundle.getString("link","");


来源:https://stackoverflow.com/questions/12840550/how-can-i-pass-data-from-activity-to-the-dialogfragment-that-activity-invoked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!