Passing an argument to DialogFragment

I need to pass some variables to DialogFragment , so I can perform the action. Eclipse suggests that I should use

 Fragment#setArguments(Bundle) 

But I do not know how to use this function. How can I use it to pass variables to my dialog?

+79
android android-dialogfragment
Mar 17 '13 at 9:20
source share
4 answers

Using newInstance

 public static MyDialogFragment newInstance(int num) { MyDialogFragment f = new MyDialogFragment(); // Supply num input as an argument. Bundle args = new Bundle(); args.putInt("num", num); f.setArguments(args); return f; } 

And we get Args like

 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mNum = getArguments().getInt("num"); ... } 

See full example here
http://developer.android.com/reference/android/app/DialogFragment.html

+207
Mar 17 '13 at 9:26
source share

as a general way of working with fragments, as JafarKhQ noted, you should not pass parameters in the constructor, but with the Bundle .

The built-in method for the Fragment class is setArguments(Bundle) and getArguments() .

basically, what you do is set up a package with all your Parcelable and send them.
in turn, your fragment will receive those elements in it onCreate and do this magic for them.

the method shown on the DialogFragment link was one way to do this in a multi-user fragment with one specific data type and works fine fine, but you can also do it manually.

+4
Mar 17 '13 at 9:45
source share

I used to send some values ​​from my list

How to send

 mListview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { Favorite clickedObj = (Favorite) parent.getItemAtPosition(position); Bundle args = new Bundle(); args.putString("tar_name", clickedObj.getNameTarife()); args.putString("fav_name", clickedObj.getName()); FragmentManager fragmentManager = getSupportFragmentManager(); TarifeDetayPopup userPopUp = new TarifeDetayPopup(); userPopUp.setArguments(args); userPopUp.show(fragmentManager, "sam"); return false; } }); 

and how to get inside the onCreate () method of the dialog box

  Bundle mArgs = getArguments(); String nameTrife = mArgs.getString("tar_name"); String nameFav = mArgs.getString("fav_name"); String name = ""; 
+2
Jun 19 '17 at 10:21
source share

In my case, none of the above code with bundle-operate works; Here is my solution (I don't know if it is the correct code or not, but it works in my case):

 public class DialogMessageType extends DialogFragment { private static String bodyText; public static DialogMessageType addSomeString(String temp){ DialogMessageType f = new DialogMessageType(); bodyText = temp; return f; }; @Override public Dialog onCreateDialog(Bundle savedInstanceState) { final String[] choiseArray = {"sms", "email"}; String title = "Send text via:"; final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setTitle(title).setItems(choiseArray, itemClickListener); builder.setCancelable(true); return builder.create(); } DialogInterface.OnClickListener itemClickListener = new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { switch (which){ case 0: prepareToSendCoordsViaSMS(bodyText); dialog.dismiss(); break; case 1: prepareToSendCoordsViaEmail(bodyText); dialog.dismiss(); break; default: break; } } }; [...] } public class SendObjectActivity extends FragmentActivity { [...] DialogMessageType dialogMessageType = DialogMessageType.addSomeString(stringToSend); dialogMessageType.show(getSupportFragmentManager(),"dialogMessageType"); [...] } 
0
Oct 08 '15 at 17:04
source share



All Articles