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\".
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
Well I did something that I don't see as a suggestion very much, so I'm posting it here for public comment in case this is very bad for some reason.
Anyhow 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.
So that's what I did. Is that horrible? Comments appreciated. TIA
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...");
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