25

I know that this was asked before, but I dont quite understand how to implement it. I have a fragment "myFragment" in which I create an object of a "myDialogueFragment". I want to pass some value to the myDialogueFragment when I invoke it from the myFragment. I have an integer num, that I want to pass to the myDialogueFragment and store that number in a local database along with some other info from the myDialogueFragment.

I might be mistaken, but all the code I have seen is about sending data from the myDialogueFragment back to the myFragment which is not what I really want.

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

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

So, this code gets the arguments within the myFragment onCreate() method. I want to sent the arguments within the myFragment() and receive them in the myDialogueFragment.

How can I achieve that?

phedon rousou
  • 1,740
  • 5
  • 17
  • 24
  • 1
    Doesn't your code achieve exactly what you want? You have mNum in your DialogFragment and you can use it throughout the lifetime of the DialogFragment, – Karakuri Jul 12 '13 at 19:32
  • the num is in the Fragment not in the DialogFragment, I want to pass the num in the DialogFragment and retrieve it from there – phedon rousou Jul 12 '13 at 19:37
  • DialogFragment extends Fragment, you can do the exact same thing in it's onCreate(). – Karakuri Jul 12 '13 at 19:38
  • hmm, I havent thought of it like that, let me try,so you are telling me that I can use the same but in the onCreateDialog().. – phedon rousou Jul 12 '13 at 19:40

4 Answers4

59

What you need is to setArguments on the fragment as follows:

Bundle args = new Bundle();
args.putString("key", "value");
DialogFragment newFragment = new YourDialogFragment();  
newFragment.setArguments(args);
newFragment.show(getSupportFragmentManager(), "TAG");

All you have to do now, is catch those arguments in your fragment and use them...

AND IN THE DIALOG FRAGMENT YOU READ IT LIKE THIS...

Bundle mArgs = getArguments();
String myValue = mArgs.getString("keyUsed to send it...");
Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
Martin Cazares
  • 13,637
  • 10
  • 47
  • 54
2

I call a FragmentDialog inside a calsswhich exdented to ActivityFragment

//TODO 1
                Followers clickedObj = (Followers) 

                parent.getItemAtPosition(position);
                Bundle bundle = new Bundle();
                bundle.putString("name", clickedObj.getFollow_name());
                bundle.putString("nick", clickedObj.getFollow_nickname());
                bundle.putString("score", clickedObj.getFollow_score());
                bundle.putString("title", clickedObj.getFollow_title());
                FragmentManager fragmentManager = getSupportFragmentManager();
                UserPopUp userPopUp = new UserPopUp();
                //TODO 1
                userPopUp.setArguments(bundle);
                userPopUp.show(fragmentManager, "followers");

and I called it on onActivityCreated in my class which extended to DialogFragment

@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        //Bundle Data Cekme başlangıç
        Bundle mArgs = getArguments();
        String userName = mArgs.getString("name");
        String userNickName = mArgs.getString("nick");
        String userTitle = mArgs.getString("title");
        String userScore = mArgs.getString("score");
        user_name.setText(userName);
        nick_name.setText(userNickName);
        challenge_title.setText(userTitle);
        user_score.setText(userScore);
        // bitiş

    }

works pretty good

Samir
  • 6,405
  • 5
  • 39
  • 42
1

Two Fragments should never communicate directly.

The recommended way to communicate between fragments is to create a shared ViewModel object. Both fragments can access the ViewModel through their containing Activity. The Fragments can update data within the ViewModel and if the data is exposed using LiveData the new state will be pushed to the other fragment as long as it is observing the LiveData from the ViewModel.

If you are unable to use a shared ViewModel to communicate between your Fragments you can manually implement a communication flow using interfaces. However this ends up being more work to implement and it is not easily reusable in other Fragments.

for more information

Mohdroid
  • 381
  • 4
  • 9
0

I extended a DialogFragment. That's it. Now it's true you can't pass it any parameters in the constructor (which you can also work around by instantiating a newInstance static function) but you can definitely add member variables with getters/setters. Now you can very easily supply your dialog with all it needs.

Dharman
  • 30,962
  • 25
  • 85
  • 135
jrs33614
  • 1
  • 1
  • 1
    This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. – borchvm May 20 '20 at 05:57