I need to pass some data from the child fragment to the parent fragment that I will be able to read when I go back to the parent fragment. In detail:
I have a FragmentActivity that calls FragmentParent. From FragmentParent I call FragmentChild like this:
FragmentChild fragmentChild = new FragmentChild();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.frl_view_container, fragmentChild);
transaction.addToBackStack(null);
ctransaction.commit();
In FragmentChild I set a string value which I need to pass back to FragmentParent and then I return back to FragmentParent.
String result = "OK";
getFragmentManager().popBackStack();
What is the best/proper way to read the result string in FragmentParent?
Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code, we have taken fragments to pass the data between two fragments.
Passing Data between fragments in Android using ViewModel: To actually pass the data between fragments, we need to create a ViewModel object with an activity scope of both the fragments, initialize the ViewModel , and set the value of the LiveData object.
Fragments were made to plug and play anywhere. All you have to do is implement the correct type of listener, but the main point is shown. In one fragment activity, call a method and pass a variable to the main activity. From the main activity you can send it to your other fragment activity if you'd like.
Steps for Sending a Bundle to a Fragment The bundle will be saved using a key/value pair, so in MainActivity. java create a String variable for the key. Create a new bundle. Use the appropriate method from the Bundle class to send your bundle.
Android architecture components solution:
In case you are using Android architecture components, it possible to share data between all Fragments
of an Activity
with a ViewModel
. Ensure ViewModelProviders
makes use of Activity
context to create ViewModels
.
public class SharedViewModel extends ViewModel {
private final MutableLiveData<Item> selected = new MutableLiveData<Item>();
public void select(Item item) {
selected.setValue(item);
}
public LiveData<Item> getSelected() {
return selected;
}
}
public class MasterFragment extends Fragment {
private SharedViewModel model;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
model = ViewModelProviders.of(getActivity()).get(SharedViewModel.class);
itemSelector.setOnClickListener(item -> {
model.select(item);
});
}
}
public class DetailFragment extends Fragment {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedViewModel model = ViewModelProviders.of(getActivity()).get(SharedViewModel.class);
model.getSelected().observe(this, { item ->
// Update the UI.
});
}
}
Non Android architecture components solution:
You can use setTargetFragment and onActivityResult to achieve this.
Set FragmentParent instance as target fragment on FragmentChild instance i.e.
FragmentChild fragmentChild = new FragmentChild();
fragmentChild.setTargetFragment(this, FRAGMENT_CODE);
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.frl_view_container, fragmentChild);
transaction.addToBackStack(null);
transaction.commit();
In FragmentChild, wherever you are invoking the popBackStack, call onActivityResult on the set target Fragment. Use Bundle to pass on additional data.
Intent intent = new Intent();
intent.putExtra(FRAGMENT_KEY, "Ok");
getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, intent);
getFragmentManager().popBackStack();
Back in FragmentParent, override the default onActivityResult method.
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == FRAGMENT_CODE && resultCode == Activity.RESULT_OK) {
if(data != null) {
String value = data.getStringExtra(FRAGMENT_KEY);
if(value != null) {
Log.v(TAG, "Data passed from Child fragment = " + value);
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With