passing argument to DialogFragment












100















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



Fragment#setArguments(Bundle)


But I don't know how to use this function. How can I use it to pass variables to my dialog?










share|improve this question

























  • Look at the sample code: androidxref.com/4.4.2_r1/xref/development/samples/ApiDemos/src/…

    – IgorGanapolsky
    Feb 12 '14 at 23:12
















100















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



Fragment#setArguments(Bundle)


But I don't know how to use this function. How can I use it to pass variables to my dialog?










share|improve this question

























  • Look at the sample code: androidxref.com/4.4.2_r1/xref/development/samples/ApiDemos/src/…

    – IgorGanapolsky
    Feb 12 '14 at 23:12














100












100








100


22






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



Fragment#setArguments(Bundle)


But I don't know how to use this function. How can I use it to pass variables to my dialog?










share|improve this question
















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



Fragment#setArguments(Bundle)


But I don't know how to use this function. How can I use it to pass variables to my dialog?







android android-dialogfragment






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 19 '15 at 12:58









yochannah

2,27553147




2,27553147










asked Mar 17 '13 at 9:20









giozhgiozh

3,6052076137




3,6052076137













  • Look at the sample code: androidxref.com/4.4.2_r1/xref/development/samples/ApiDemos/src/…

    – IgorGanapolsky
    Feb 12 '14 at 23:12



















  • Look at the sample code: androidxref.com/4.4.2_r1/xref/development/samples/ApiDemos/src/…

    – IgorGanapolsky
    Feb 12 '14 at 23:12

















Look at the sample code: androidxref.com/4.4.2_r1/xref/development/samples/ApiDemos/src/…

– IgorGanapolsky
Feb 12 '14 at 23:12





Look at the sample code: androidxref.com/4.4.2_r1/xref/development/samples/ApiDemos/src/…

– IgorGanapolsky
Feb 12 '14 at 23:12












6 Answers
6






active

oldest

votes


















260














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 get the Args like this



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


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






share|improve this answer


























  • Can you just set private variables on MyDialogFragment instead of using bundle?

    – SIr Codealot
    Feb 19 '15 at 0:56






  • 8





    @SIrCodealot the effect would be the same as setting variables on Activity or Fragment. If you face something that destroys and recreates the DialogDragment like rotation change, you will lose all variables.

    – inmyth
    Mar 16 '15 at 4:49






  • 1





    For all those wondering why an overloaded constructor isn't used in this case, see another discussion on the topic that is highly instructive: stackoverflow.com/questions/14011808/…

    – HondaGuy
    May 31 '17 at 20:05











  • Took me a minute to notice that the savedInstanceState is not used.

    – Odys
    May 31 '18 at 11:32



















10














I used to send some values from my listview



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;
}
});


How to receive inside onCreate() method of DialogFragment



    Bundle mArgs = getArguments();
String nameTrife = mArgs.getString("tar_name");
String nameFav = mArgs.getString("fav_name");
String name = "";


// Kotlin upload



 val fm = supportFragmentManager
val dialogFragment = AddProgFargmentDialog() // my custom FargmentDialog
var args: Bundle? = null
args?.putString("title", model.title);
dialogFragment.setArguments(args)
dialogFragment.show(fm, "Sample Fragment")


// receive



 override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (getArguments() != null) {
val mArgs = arguments
var myDay= mArgs.getString("title")
}
}





share|improve this answer





















  • 1





    The Better answer!

    – user55924
    Mar 11 '18 at 14:40



















5














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



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



basically, what you do is set up a bundle with all your Parcelable items and send them on.
in turn, your Fragment will get those items in it's onCreate and do it's magic to them.



the way shown in the DialogFragment link was one way of doing this in a multi appearing fragment with one specific type of data and works fine most of the time, but you can also do this manually.






share|improve this answer































    3














    So there is two ways to pass values from fragment/activity to dialog fragment:-





    1. Create dialog fragment object with make setter method and pass value/argument.


    2. Pass value/argument through bundle.





    Method 1:



    // Fragment or Activity 
    @Override
    public void onClick(View v) {
    DialogFragmentWithSetter dialog = new DialogFragmentWithSetter();
    dialog.setValue(header, body);
    dialog.show(getSupportFragmentManager(), "DialogFragmentWithSetter");
    }


    // your dialog fragment
    public class MyDialogFragment extends DialogFragment {
    String header;
    String body;
    public void setValue(String header, String body) {
    this.header = header;
    this.body = body;
    }
    // use above variable into your dialog fragment
    }



    Note:- This is not best way to do




    Method 2:



    // Fragment or Activity 
    @Override
    public void onClick(View v) {
    DialogFragmentWithSetter dialog = new DialogFragmentWithSetter();

    Bundle bundle = new Bundle();
    bundle.putString("header", "Header");
    bundle.putString("body", "Body");
    dialog.setArguments(bundle);
    dialog.show(getSupportFragmentManager(), "DialogFragmentWithSetter");
    }


    // your dialog fragment
    public class MyDialogFragment extends DialogFragment {
    String header;
    String body;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
    header = getArguments().getString("header","");
    body = getArguments().getString("body","");
    }
    }
    // use above variable into your dialog fragment
    }



    Note:- This is the best way to do.







    share|improve this answer
























    • You can even use the Gson library to pass Objects or ArrayList<Objects> as Strings in the Bundle.

      – Jeffrey
      Aug 11 '18 at 7:30





















    0














    In my case, none of the code above with bundle-operate works; Here is my decision (I don't know if it is proper 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");

    [...]
    }





    share|improve this answer


























    • 1) By storing the bodyText statically, you effectively make it impossible to have two instances of this class at the same time, with different body texts. There's no reason not to store it as an instance variable instead. 2) The whole purpose of sending arguments using setArguments(Bundle) is that the OS can then re-create the fragment in case it's lost in a low memory situation etc. With your solution the fragment will be re-created, and body text will be whatever the last instance of the dialog used (since it's static). The correct solution is to set the body text as a bundle parameter.

      – JHH
      Oct 26 '16 at 10:52



















    0














    Just that i want to show how to do what do said @JafarKhQ in Kotlin for those who use kotlin that might help them and save theme time too:



    so you have to create a companion objet to create new newInstance function



    you can set the paremter of the function whatever you want.
    using



     val args = Bundle()


    you can set your args.



    You can now use args.putSomthing to add you args which u give as a prameter in your newInstance function.
    putString(key:String,str:String) to add string for example and so on



    Now to get the argument you can use
    arguments.getSomthing(Key:String)=> like arguments.getString("1")



    here is a full example



    class IntervModifFragment : DialogFragment(), ModContract.View
    {
    companion object {
    fun newInstance( plom:String,type:String,position: Int):IntervModifFragment {
    val fragment =IntervModifFragment()
    val args = Bundle()
    args.putString( "1",plom)
    args.putString("2",type)
    args.putInt("3",position)
    fragment.arguments = args
    return fragment
    }
    }

    ...
    override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    fillSpinerPlom(view,arguments.getString("1"))
    fillSpinerType(view, arguments.getString("2"))
    confirmer_virme.setOnClickListener({on_confirmClick( arguments.getInt("3"))})


    val dateSetListener = object : DatePickerDialog.OnDateSetListener {
    override fun onDateSet(view: DatePicker, year: Int, monthOfYear: Int,
    dayOfMonth: Int) {
    val datep= DateT(year,monthOfYear,dayOfMonth)
    updateDateInView(datep.date)
    }
    }

    }
    ...
    }


    Now how to create your dialog you can do somthing like this in another class



      val dialog = IntervModifFragment.newInstance(ListInter.list[position].plom,ListInter.list[position].type,position)


    like this for example



    class InterListAdapter(private val context: Context, linkedList: LinkedList<InterItem> ) : RecyclerView.Adapter<InterListAdapter.ViewHolder>()
    {
    ...
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {

    ...
    holder.btn_update!!.setOnClickListener {
    val dialog = IntervModifFragment.newInstance(ListInter.list[position].plom,ListInter.list[position].type,position)
    val ft = (context as AppCompatActivity).supportFragmentManager.beginTransaction()
    dialog.show(ft, ContentValues.TAG)
    }
    ...
    }
    ..

    }





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f15459209%2fpassing-argument-to-dialogfragment%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      6 Answers
      6






      active

      oldest

      votes








      6 Answers
      6






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      260














      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 get the Args like this



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


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






      share|improve this answer


























      • Can you just set private variables on MyDialogFragment instead of using bundle?

        – SIr Codealot
        Feb 19 '15 at 0:56






      • 8





        @SIrCodealot the effect would be the same as setting variables on Activity or Fragment. If you face something that destroys and recreates the DialogDragment like rotation change, you will lose all variables.

        – inmyth
        Mar 16 '15 at 4:49






      • 1





        For all those wondering why an overloaded constructor isn't used in this case, see another discussion on the topic that is highly instructive: stackoverflow.com/questions/14011808/…

        – HondaGuy
        May 31 '17 at 20:05











      • Took me a minute to notice that the savedInstanceState is not used.

        – Odys
        May 31 '18 at 11:32
















      260














      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 get the Args like this



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


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






      share|improve this answer


























      • Can you just set private variables on MyDialogFragment instead of using bundle?

        – SIr Codealot
        Feb 19 '15 at 0:56






      • 8





        @SIrCodealot the effect would be the same as setting variables on Activity or Fragment. If you face something that destroys and recreates the DialogDragment like rotation change, you will lose all variables.

        – inmyth
        Mar 16 '15 at 4:49






      • 1





        For all those wondering why an overloaded constructor isn't used in this case, see another discussion on the topic that is highly instructive: stackoverflow.com/questions/14011808/…

        – HondaGuy
        May 31 '17 at 20:05











      • Took me a minute to notice that the savedInstanceState is not used.

        – Odys
        May 31 '18 at 11:32














      260












      260








      260







      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 get the Args like this



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


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






      share|improve this answer















      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 get the Args like this



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


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







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Oct 7 '15 at 19:28









      Adam Varhegyi

      5,5322891166




      5,5322891166










      answered Mar 17 '13 at 9:26









      JafarKhQJafarKhQ

      7,53122940




      7,53122940













      • Can you just set private variables on MyDialogFragment instead of using bundle?

        – SIr Codealot
        Feb 19 '15 at 0:56






      • 8





        @SIrCodealot the effect would be the same as setting variables on Activity or Fragment. If you face something that destroys and recreates the DialogDragment like rotation change, you will lose all variables.

        – inmyth
        Mar 16 '15 at 4:49






      • 1





        For all those wondering why an overloaded constructor isn't used in this case, see another discussion on the topic that is highly instructive: stackoverflow.com/questions/14011808/…

        – HondaGuy
        May 31 '17 at 20:05











      • Took me a minute to notice that the savedInstanceState is not used.

        – Odys
        May 31 '18 at 11:32



















      • Can you just set private variables on MyDialogFragment instead of using bundle?

        – SIr Codealot
        Feb 19 '15 at 0:56






      • 8





        @SIrCodealot the effect would be the same as setting variables on Activity or Fragment. If you face something that destroys and recreates the DialogDragment like rotation change, you will lose all variables.

        – inmyth
        Mar 16 '15 at 4:49






      • 1





        For all those wondering why an overloaded constructor isn't used in this case, see another discussion on the topic that is highly instructive: stackoverflow.com/questions/14011808/…

        – HondaGuy
        May 31 '17 at 20:05











      • Took me a minute to notice that the savedInstanceState is not used.

        – Odys
        May 31 '18 at 11:32

















      Can you just set private variables on MyDialogFragment instead of using bundle?

      – SIr Codealot
      Feb 19 '15 at 0:56





      Can you just set private variables on MyDialogFragment instead of using bundle?

      – SIr Codealot
      Feb 19 '15 at 0:56




      8




      8





      @SIrCodealot the effect would be the same as setting variables on Activity or Fragment. If you face something that destroys and recreates the DialogDragment like rotation change, you will lose all variables.

      – inmyth
      Mar 16 '15 at 4:49





      @SIrCodealot the effect would be the same as setting variables on Activity or Fragment. If you face something that destroys and recreates the DialogDragment like rotation change, you will lose all variables.

      – inmyth
      Mar 16 '15 at 4:49




      1




      1





      For all those wondering why an overloaded constructor isn't used in this case, see another discussion on the topic that is highly instructive: stackoverflow.com/questions/14011808/…

      – HondaGuy
      May 31 '17 at 20:05





      For all those wondering why an overloaded constructor isn't used in this case, see another discussion on the topic that is highly instructive: stackoverflow.com/questions/14011808/…

      – HondaGuy
      May 31 '17 at 20:05













      Took me a minute to notice that the savedInstanceState is not used.

      – Odys
      May 31 '18 at 11:32





      Took me a minute to notice that the savedInstanceState is not used.

      – Odys
      May 31 '18 at 11:32













      10














      I used to send some values from my listview



      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;
      }
      });


      How to receive inside onCreate() method of DialogFragment



          Bundle mArgs = getArguments();
      String nameTrife = mArgs.getString("tar_name");
      String nameFav = mArgs.getString("fav_name");
      String name = "";


      // Kotlin upload



       val fm = supportFragmentManager
      val dialogFragment = AddProgFargmentDialog() // my custom FargmentDialog
      var args: Bundle? = null
      args?.putString("title", model.title);
      dialogFragment.setArguments(args)
      dialogFragment.show(fm, "Sample Fragment")


      // receive



       override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      if (getArguments() != null) {
      val mArgs = arguments
      var myDay= mArgs.getString("title")
      }
      }





      share|improve this answer





















      • 1





        The Better answer!

        – user55924
        Mar 11 '18 at 14:40
















      10














      I used to send some values from my listview



      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;
      }
      });


      How to receive inside onCreate() method of DialogFragment



          Bundle mArgs = getArguments();
      String nameTrife = mArgs.getString("tar_name");
      String nameFav = mArgs.getString("fav_name");
      String name = "";


      // Kotlin upload



       val fm = supportFragmentManager
      val dialogFragment = AddProgFargmentDialog() // my custom FargmentDialog
      var args: Bundle? = null
      args?.putString("title", model.title);
      dialogFragment.setArguments(args)
      dialogFragment.show(fm, "Sample Fragment")


      // receive



       override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      if (getArguments() != null) {
      val mArgs = arguments
      var myDay= mArgs.getString("title")
      }
      }





      share|improve this answer





















      • 1





        The Better answer!

        – user55924
        Mar 11 '18 at 14:40














      10












      10








      10







      I used to send some values from my listview



      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;
      }
      });


      How to receive inside onCreate() method of DialogFragment



          Bundle mArgs = getArguments();
      String nameTrife = mArgs.getString("tar_name");
      String nameFav = mArgs.getString("fav_name");
      String name = "";


      // Kotlin upload



       val fm = supportFragmentManager
      val dialogFragment = AddProgFargmentDialog() // my custom FargmentDialog
      var args: Bundle? = null
      args?.putString("title", model.title);
      dialogFragment.setArguments(args)
      dialogFragment.show(fm, "Sample Fragment")


      // receive



       override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      if (getArguments() != null) {
      val mArgs = arguments
      var myDay= mArgs.getString("title")
      }
      }





      share|improve this answer















      I used to send some values from my listview



      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;
      }
      });


      How to receive inside onCreate() method of DialogFragment



          Bundle mArgs = getArguments();
      String nameTrife = mArgs.getString("tar_name");
      String nameFav = mArgs.getString("fav_name");
      String name = "";


      // Kotlin upload



       val fm = supportFragmentManager
      val dialogFragment = AddProgFargmentDialog() // my custom FargmentDialog
      var args: Bundle? = null
      args?.putString("title", model.title);
      dialogFragment.setArguments(args)
      dialogFragment.show(fm, "Sample Fragment")


      // receive



       override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      if (getArguments() != null) {
      val mArgs = arguments
      var myDay= mArgs.getString("title")
      }
      }






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Oct 20 '18 at 7:56

























      answered Jun 19 '17 at 10:21









      SamSam

      2,36342331




      2,36342331








      • 1





        The Better answer!

        – user55924
        Mar 11 '18 at 14:40














      • 1





        The Better answer!

        – user55924
        Mar 11 '18 at 14:40








      1




      1





      The Better answer!

      – user55924
      Mar 11 '18 at 14:40





      The Better answer!

      – user55924
      Mar 11 '18 at 14:40











      5














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



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



      basically, what you do is set up a bundle with all your Parcelable items and send them on.
      in turn, your Fragment will get those items in it's onCreate and do it's magic to them.



      the way shown in the DialogFragment link was one way of doing this in a multi appearing fragment with one specific type of data and works fine most of the time, but you can also do this manually.






      share|improve this answer




























        5














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



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



        basically, what you do is set up a bundle with all your Parcelable items and send them on.
        in turn, your Fragment will get those items in it's onCreate and do it's magic to them.



        the way shown in the DialogFragment link was one way of doing this in a multi appearing fragment with one specific type of data and works fine most of the time, but you can also do this manually.






        share|improve this answer


























          5












          5








          5







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



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



          basically, what you do is set up a bundle with all your Parcelable items and send them on.
          in turn, your Fragment will get those items in it's onCreate and do it's magic to them.



          the way shown in the DialogFragment link was one way of doing this in a multi appearing fragment with one specific type of data and works fine most of the time, but you can also do this manually.






          share|improve this answer













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



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



          basically, what you do is set up a bundle with all your Parcelable items and send them on.
          in turn, your Fragment will get those items in it's onCreate and do it's magic to them.



          the way shown in the DialogFragment link was one way of doing this in a multi appearing fragment with one specific type of data and works fine most of the time, but you can also do this manually.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 17 '13 at 9:45









          thepooshthepoosh

          9,4121359119




          9,4121359119























              3














              So there is two ways to pass values from fragment/activity to dialog fragment:-





              1. Create dialog fragment object with make setter method and pass value/argument.


              2. Pass value/argument through bundle.





              Method 1:



              // Fragment or Activity 
              @Override
              public void onClick(View v) {
              DialogFragmentWithSetter dialog = new DialogFragmentWithSetter();
              dialog.setValue(header, body);
              dialog.show(getSupportFragmentManager(), "DialogFragmentWithSetter");
              }


              // your dialog fragment
              public class MyDialogFragment extends DialogFragment {
              String header;
              String body;
              public void setValue(String header, String body) {
              this.header = header;
              this.body = body;
              }
              // use above variable into your dialog fragment
              }



              Note:- This is not best way to do




              Method 2:



              // Fragment or Activity 
              @Override
              public void onClick(View v) {
              DialogFragmentWithSetter dialog = new DialogFragmentWithSetter();

              Bundle bundle = new Bundle();
              bundle.putString("header", "Header");
              bundle.putString("body", "Body");
              dialog.setArguments(bundle);
              dialog.show(getSupportFragmentManager(), "DialogFragmentWithSetter");
              }


              // your dialog fragment
              public class MyDialogFragment extends DialogFragment {
              String header;
              String body;

              @Override
              public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              if (getArguments() != null) {
              header = getArguments().getString("header","");
              body = getArguments().getString("body","");
              }
              }
              // use above variable into your dialog fragment
              }



              Note:- This is the best way to do.







              share|improve this answer
























              • You can even use the Gson library to pass Objects or ArrayList<Objects> as Strings in the Bundle.

                – Jeffrey
                Aug 11 '18 at 7:30


















              3














              So there is two ways to pass values from fragment/activity to dialog fragment:-





              1. Create dialog fragment object with make setter method and pass value/argument.


              2. Pass value/argument through bundle.





              Method 1:



              // Fragment or Activity 
              @Override
              public void onClick(View v) {
              DialogFragmentWithSetter dialog = new DialogFragmentWithSetter();
              dialog.setValue(header, body);
              dialog.show(getSupportFragmentManager(), "DialogFragmentWithSetter");
              }


              // your dialog fragment
              public class MyDialogFragment extends DialogFragment {
              String header;
              String body;
              public void setValue(String header, String body) {
              this.header = header;
              this.body = body;
              }
              // use above variable into your dialog fragment
              }



              Note:- This is not best way to do




              Method 2:



              // Fragment or Activity 
              @Override
              public void onClick(View v) {
              DialogFragmentWithSetter dialog = new DialogFragmentWithSetter();

              Bundle bundle = new Bundle();
              bundle.putString("header", "Header");
              bundle.putString("body", "Body");
              dialog.setArguments(bundle);
              dialog.show(getSupportFragmentManager(), "DialogFragmentWithSetter");
              }


              // your dialog fragment
              public class MyDialogFragment extends DialogFragment {
              String header;
              String body;

              @Override
              public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              if (getArguments() != null) {
              header = getArguments().getString("header","");
              body = getArguments().getString("body","");
              }
              }
              // use above variable into your dialog fragment
              }



              Note:- This is the best way to do.







              share|improve this answer
























              • You can even use the Gson library to pass Objects or ArrayList<Objects> as Strings in the Bundle.

                – Jeffrey
                Aug 11 '18 at 7:30
















              3












              3








              3







              So there is two ways to pass values from fragment/activity to dialog fragment:-





              1. Create dialog fragment object with make setter method and pass value/argument.


              2. Pass value/argument through bundle.





              Method 1:



              // Fragment or Activity 
              @Override
              public void onClick(View v) {
              DialogFragmentWithSetter dialog = new DialogFragmentWithSetter();
              dialog.setValue(header, body);
              dialog.show(getSupportFragmentManager(), "DialogFragmentWithSetter");
              }


              // your dialog fragment
              public class MyDialogFragment extends DialogFragment {
              String header;
              String body;
              public void setValue(String header, String body) {
              this.header = header;
              this.body = body;
              }
              // use above variable into your dialog fragment
              }



              Note:- This is not best way to do




              Method 2:



              // Fragment or Activity 
              @Override
              public void onClick(View v) {
              DialogFragmentWithSetter dialog = new DialogFragmentWithSetter();

              Bundle bundle = new Bundle();
              bundle.putString("header", "Header");
              bundle.putString("body", "Body");
              dialog.setArguments(bundle);
              dialog.show(getSupportFragmentManager(), "DialogFragmentWithSetter");
              }


              // your dialog fragment
              public class MyDialogFragment extends DialogFragment {
              String header;
              String body;

              @Override
              public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              if (getArguments() != null) {
              header = getArguments().getString("header","");
              body = getArguments().getString("body","");
              }
              }
              // use above variable into your dialog fragment
              }



              Note:- This is the best way to do.







              share|improve this answer













              So there is two ways to pass values from fragment/activity to dialog fragment:-





              1. Create dialog fragment object with make setter method and pass value/argument.


              2. Pass value/argument through bundle.





              Method 1:



              // Fragment or Activity 
              @Override
              public void onClick(View v) {
              DialogFragmentWithSetter dialog = new DialogFragmentWithSetter();
              dialog.setValue(header, body);
              dialog.show(getSupportFragmentManager(), "DialogFragmentWithSetter");
              }


              // your dialog fragment
              public class MyDialogFragment extends DialogFragment {
              String header;
              String body;
              public void setValue(String header, String body) {
              this.header = header;
              this.body = body;
              }
              // use above variable into your dialog fragment
              }



              Note:- This is not best way to do




              Method 2:



              // Fragment or Activity 
              @Override
              public void onClick(View v) {
              DialogFragmentWithSetter dialog = new DialogFragmentWithSetter();

              Bundle bundle = new Bundle();
              bundle.putString("header", "Header");
              bundle.putString("body", "Body");
              dialog.setArguments(bundle);
              dialog.show(getSupportFragmentManager(), "DialogFragmentWithSetter");
              }


              // your dialog fragment
              public class MyDialogFragment extends DialogFragment {
              String header;
              String body;

              @Override
              public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              if (getArguments() != null) {
              header = getArguments().getString("header","");
              body = getArguments().getString("body","");
              }
              }
              // use above variable into your dialog fragment
              }



              Note:- This is the best way to do.








              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Feb 9 '18 at 10:00









              dugguduggu

              29.7k99099




              29.7k99099













              • You can even use the Gson library to pass Objects or ArrayList<Objects> as Strings in the Bundle.

                – Jeffrey
                Aug 11 '18 at 7:30





















              • You can even use the Gson library to pass Objects or ArrayList<Objects> as Strings in the Bundle.

                – Jeffrey
                Aug 11 '18 at 7:30



















              You can even use the Gson library to pass Objects or ArrayList<Objects> as Strings in the Bundle.

              – Jeffrey
              Aug 11 '18 at 7:30







              You can even use the Gson library to pass Objects or ArrayList<Objects> as Strings in the Bundle.

              – Jeffrey
              Aug 11 '18 at 7:30













              0














              In my case, none of the code above with bundle-operate works; Here is my decision (I don't know if it is proper 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");

              [...]
              }





              share|improve this answer


























              • 1) By storing the bodyText statically, you effectively make it impossible to have two instances of this class at the same time, with different body texts. There's no reason not to store it as an instance variable instead. 2) The whole purpose of sending arguments using setArguments(Bundle) is that the OS can then re-create the fragment in case it's lost in a low memory situation etc. With your solution the fragment will be re-created, and body text will be whatever the last instance of the dialog used (since it's static). The correct solution is to set the body text as a bundle parameter.

                – JHH
                Oct 26 '16 at 10:52
















              0














              In my case, none of the code above with bundle-operate works; Here is my decision (I don't know if it is proper 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");

              [...]
              }





              share|improve this answer


























              • 1) By storing the bodyText statically, you effectively make it impossible to have two instances of this class at the same time, with different body texts. There's no reason not to store it as an instance variable instead. 2) The whole purpose of sending arguments using setArguments(Bundle) is that the OS can then re-create the fragment in case it's lost in a low memory situation etc. With your solution the fragment will be re-created, and body text will be whatever the last instance of the dialog used (since it's static). The correct solution is to set the body text as a bundle parameter.

                – JHH
                Oct 26 '16 at 10:52














              0












              0








              0







              In my case, none of the code above with bundle-operate works; Here is my decision (I don't know if it is proper 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");

              [...]
              }





              share|improve this answer















              In my case, none of the code above with bundle-operate works; Here is my decision (I don't know if it is proper 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");

              [...]
              }






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Oct 8 '15 at 17:28









              admdrew

              3,18241936




              3,18241936










              answered Oct 8 '15 at 17:04









              KirillKirill

              11




              11













              • 1) By storing the bodyText statically, you effectively make it impossible to have two instances of this class at the same time, with different body texts. There's no reason not to store it as an instance variable instead. 2) The whole purpose of sending arguments using setArguments(Bundle) is that the OS can then re-create the fragment in case it's lost in a low memory situation etc. With your solution the fragment will be re-created, and body text will be whatever the last instance of the dialog used (since it's static). The correct solution is to set the body text as a bundle parameter.

                – JHH
                Oct 26 '16 at 10:52



















              • 1) By storing the bodyText statically, you effectively make it impossible to have two instances of this class at the same time, with different body texts. There's no reason not to store it as an instance variable instead. 2) The whole purpose of sending arguments using setArguments(Bundle) is that the OS can then re-create the fragment in case it's lost in a low memory situation etc. With your solution the fragment will be re-created, and body text will be whatever the last instance of the dialog used (since it's static). The correct solution is to set the body text as a bundle parameter.

                – JHH
                Oct 26 '16 at 10:52

















              1) By storing the bodyText statically, you effectively make it impossible to have two instances of this class at the same time, with different body texts. There's no reason not to store it as an instance variable instead. 2) The whole purpose of sending arguments using setArguments(Bundle) is that the OS can then re-create the fragment in case it's lost in a low memory situation etc. With your solution the fragment will be re-created, and body text will be whatever the last instance of the dialog used (since it's static). The correct solution is to set the body text as a bundle parameter.

              – JHH
              Oct 26 '16 at 10:52





              1) By storing the bodyText statically, you effectively make it impossible to have two instances of this class at the same time, with different body texts. There's no reason not to store it as an instance variable instead. 2) The whole purpose of sending arguments using setArguments(Bundle) is that the OS can then re-create the fragment in case it's lost in a low memory situation etc. With your solution the fragment will be re-created, and body text will be whatever the last instance of the dialog used (since it's static). The correct solution is to set the body text as a bundle parameter.

              – JHH
              Oct 26 '16 at 10:52











              0














              Just that i want to show how to do what do said @JafarKhQ in Kotlin for those who use kotlin that might help them and save theme time too:



              so you have to create a companion objet to create new newInstance function



              you can set the paremter of the function whatever you want.
              using



               val args = Bundle()


              you can set your args.



              You can now use args.putSomthing to add you args which u give as a prameter in your newInstance function.
              putString(key:String,str:String) to add string for example and so on



              Now to get the argument you can use
              arguments.getSomthing(Key:String)=> like arguments.getString("1")



              here is a full example



              class IntervModifFragment : DialogFragment(), ModContract.View
              {
              companion object {
              fun newInstance( plom:String,type:String,position: Int):IntervModifFragment {
              val fragment =IntervModifFragment()
              val args = Bundle()
              args.putString( "1",plom)
              args.putString("2",type)
              args.putInt("3",position)
              fragment.arguments = args
              return fragment
              }
              }

              ...
              override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
              super.onViewCreated(view, savedInstanceState)
              fillSpinerPlom(view,arguments.getString("1"))
              fillSpinerType(view, arguments.getString("2"))
              confirmer_virme.setOnClickListener({on_confirmClick( arguments.getInt("3"))})


              val dateSetListener = object : DatePickerDialog.OnDateSetListener {
              override fun onDateSet(view: DatePicker, year: Int, monthOfYear: Int,
              dayOfMonth: Int) {
              val datep= DateT(year,monthOfYear,dayOfMonth)
              updateDateInView(datep.date)
              }
              }

              }
              ...
              }


              Now how to create your dialog you can do somthing like this in another class



                val dialog = IntervModifFragment.newInstance(ListInter.list[position].plom,ListInter.list[position].type,position)


              like this for example



              class InterListAdapter(private val context: Context, linkedList: LinkedList<InterItem> ) : RecyclerView.Adapter<InterListAdapter.ViewHolder>()
              {
              ...
              override fun onBindViewHolder(holder: ViewHolder, position: Int) {

              ...
              holder.btn_update!!.setOnClickListener {
              val dialog = IntervModifFragment.newInstance(ListInter.list[position].plom,ListInter.list[position].type,position)
              val ft = (context as AppCompatActivity).supportFragmentManager.beginTransaction()
              dialog.show(ft, ContentValues.TAG)
              }
              ...
              }
              ..

              }





              share|improve this answer




























                0














                Just that i want to show how to do what do said @JafarKhQ in Kotlin for those who use kotlin that might help them and save theme time too:



                so you have to create a companion objet to create new newInstance function



                you can set the paremter of the function whatever you want.
                using



                 val args = Bundle()


                you can set your args.



                You can now use args.putSomthing to add you args which u give as a prameter in your newInstance function.
                putString(key:String,str:String) to add string for example and so on



                Now to get the argument you can use
                arguments.getSomthing(Key:String)=> like arguments.getString("1")



                here is a full example



                class IntervModifFragment : DialogFragment(), ModContract.View
                {
                companion object {
                fun newInstance( plom:String,type:String,position: Int):IntervModifFragment {
                val fragment =IntervModifFragment()
                val args = Bundle()
                args.putString( "1",plom)
                args.putString("2",type)
                args.putInt("3",position)
                fragment.arguments = args
                return fragment
                }
                }

                ...
                override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
                super.onViewCreated(view, savedInstanceState)
                fillSpinerPlom(view,arguments.getString("1"))
                fillSpinerType(view, arguments.getString("2"))
                confirmer_virme.setOnClickListener({on_confirmClick( arguments.getInt("3"))})


                val dateSetListener = object : DatePickerDialog.OnDateSetListener {
                override fun onDateSet(view: DatePicker, year: Int, monthOfYear: Int,
                dayOfMonth: Int) {
                val datep= DateT(year,monthOfYear,dayOfMonth)
                updateDateInView(datep.date)
                }
                }

                }
                ...
                }


                Now how to create your dialog you can do somthing like this in another class



                  val dialog = IntervModifFragment.newInstance(ListInter.list[position].plom,ListInter.list[position].type,position)


                like this for example



                class InterListAdapter(private val context: Context, linkedList: LinkedList<InterItem> ) : RecyclerView.Adapter<InterListAdapter.ViewHolder>()
                {
                ...
                override fun onBindViewHolder(holder: ViewHolder, position: Int) {

                ...
                holder.btn_update!!.setOnClickListener {
                val dialog = IntervModifFragment.newInstance(ListInter.list[position].plom,ListInter.list[position].type,position)
                val ft = (context as AppCompatActivity).supportFragmentManager.beginTransaction()
                dialog.show(ft, ContentValues.TAG)
                }
                ...
                }
                ..

                }





                share|improve this answer


























                  0












                  0








                  0







                  Just that i want to show how to do what do said @JafarKhQ in Kotlin for those who use kotlin that might help them and save theme time too:



                  so you have to create a companion objet to create new newInstance function



                  you can set the paremter of the function whatever you want.
                  using



                   val args = Bundle()


                  you can set your args.



                  You can now use args.putSomthing to add you args which u give as a prameter in your newInstance function.
                  putString(key:String,str:String) to add string for example and so on



                  Now to get the argument you can use
                  arguments.getSomthing(Key:String)=> like arguments.getString("1")



                  here is a full example



                  class IntervModifFragment : DialogFragment(), ModContract.View
                  {
                  companion object {
                  fun newInstance( plom:String,type:String,position: Int):IntervModifFragment {
                  val fragment =IntervModifFragment()
                  val args = Bundle()
                  args.putString( "1",plom)
                  args.putString("2",type)
                  args.putInt("3",position)
                  fragment.arguments = args
                  return fragment
                  }
                  }

                  ...
                  override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
                  super.onViewCreated(view, savedInstanceState)
                  fillSpinerPlom(view,arguments.getString("1"))
                  fillSpinerType(view, arguments.getString("2"))
                  confirmer_virme.setOnClickListener({on_confirmClick( arguments.getInt("3"))})


                  val dateSetListener = object : DatePickerDialog.OnDateSetListener {
                  override fun onDateSet(view: DatePicker, year: Int, monthOfYear: Int,
                  dayOfMonth: Int) {
                  val datep= DateT(year,monthOfYear,dayOfMonth)
                  updateDateInView(datep.date)
                  }
                  }

                  }
                  ...
                  }


                  Now how to create your dialog you can do somthing like this in another class



                    val dialog = IntervModifFragment.newInstance(ListInter.list[position].plom,ListInter.list[position].type,position)


                  like this for example



                  class InterListAdapter(private val context: Context, linkedList: LinkedList<InterItem> ) : RecyclerView.Adapter<InterListAdapter.ViewHolder>()
                  {
                  ...
                  override fun onBindViewHolder(holder: ViewHolder, position: Int) {

                  ...
                  holder.btn_update!!.setOnClickListener {
                  val dialog = IntervModifFragment.newInstance(ListInter.list[position].plom,ListInter.list[position].type,position)
                  val ft = (context as AppCompatActivity).supportFragmentManager.beginTransaction()
                  dialog.show(ft, ContentValues.TAG)
                  }
                  ...
                  }
                  ..

                  }





                  share|improve this answer













                  Just that i want to show how to do what do said @JafarKhQ in Kotlin for those who use kotlin that might help them and save theme time too:



                  so you have to create a companion objet to create new newInstance function



                  you can set the paremter of the function whatever you want.
                  using



                   val args = Bundle()


                  you can set your args.



                  You can now use args.putSomthing to add you args which u give as a prameter in your newInstance function.
                  putString(key:String,str:String) to add string for example and so on



                  Now to get the argument you can use
                  arguments.getSomthing(Key:String)=> like arguments.getString("1")



                  here is a full example



                  class IntervModifFragment : DialogFragment(), ModContract.View
                  {
                  companion object {
                  fun newInstance( plom:String,type:String,position: Int):IntervModifFragment {
                  val fragment =IntervModifFragment()
                  val args = Bundle()
                  args.putString( "1",plom)
                  args.putString("2",type)
                  args.putInt("3",position)
                  fragment.arguments = args
                  return fragment
                  }
                  }

                  ...
                  override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
                  super.onViewCreated(view, savedInstanceState)
                  fillSpinerPlom(view,arguments.getString("1"))
                  fillSpinerType(view, arguments.getString("2"))
                  confirmer_virme.setOnClickListener({on_confirmClick( arguments.getInt("3"))})


                  val dateSetListener = object : DatePickerDialog.OnDateSetListener {
                  override fun onDateSet(view: DatePicker, year: Int, monthOfYear: Int,
                  dayOfMonth: Int) {
                  val datep= DateT(year,monthOfYear,dayOfMonth)
                  updateDateInView(datep.date)
                  }
                  }

                  }
                  ...
                  }


                  Now how to create your dialog you can do somthing like this in another class



                    val dialog = IntervModifFragment.newInstance(ListInter.list[position].plom,ListInter.list[position].type,position)


                  like this for example



                  class InterListAdapter(private val context: Context, linkedList: LinkedList<InterItem> ) : RecyclerView.Adapter<InterListAdapter.ViewHolder>()
                  {
                  ...
                  override fun onBindViewHolder(holder: ViewHolder, position: Int) {

                  ...
                  holder.btn_update!!.setOnClickListener {
                  val dialog = IntervModifFragment.newInstance(ListInter.list[position].plom,ListInter.list[position].type,position)
                  val ft = (context as AppCompatActivity).supportFragmentManager.beginTransaction()
                  dialog.show(ft, ContentValues.TAG)
                  }
                  ...
                  }
                  ..

                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered May 18 '18 at 15:17









                  DINA TAKLITDINA TAKLIT

                  717722




                  717722






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f15459209%2fpassing-argument-to-dialogfragment%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Monofisismo

                      Angular Downloading a file using contenturl with Basic Authentication

                      Olmecas