I am trying to get my app to display different text each time a button is pressed. The first time I press any button it works fine, but afterwords it reverts my textviews on the fragment to the default text I set on the layout xml file. Here is the code for the buttons, as well as the function I am using to load data to the fragment:
@Override
public void onClick(View view) {
String title = "Bildad Kaggia";
String history = "Bildad Mwaganu Kaggia (1921 – 7 March 2005) was a Kenyan nationalist, activist, and politician. Kaggia was a member of the Mau Mau Central Committee. After independence he became a Member of Parliament. He established himself as a militant, fiery nationalist who wanted to serve the poor and landless people. Because of this he fell out irreconcilably with Jomo Kenyatta. ";
ReplaceFragment(new gridLayout(),title, history);
}
});```
Here is the ReplaceFragment Function:
```public void ReplaceFragment (Fragment fragment, String title, String history){
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
fragmentManager.clearFragmentResult("history");
fragmentManager.clearFragmentResult("title");
Bundle hstry = new Bundle();
hstry.putString("hstry",history);
Bundle tl = new Bundle();
tl.putString("tl",title);
fragmentManager.setFragmentResult("history",hstry);
fragmentManager.setFragmentResult("title",tl);
fragmentTransaction.replace(R.id.frameLayout,fragment);
fragmentTransaction.commitNow();
}```
And here is my fragment code:
```View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_grid_layout, container, false);
getParentFragmentManager().setFragmentResultListener("title", this, new FragmentResultListener() {
@Override
public void onFragmentResult(@NonNull String requestKey, @NonNull Bundle result) {
TextView tv1 = view.findViewById(R.id.tvTitle);
String tl=result.getString("tl");
tv1.setText(tl);
}
});
getParentFragmentManager().setFragmentResultListener("history", this, new FragmentResultListener() {
@Override
public void onFragmentResult(@NonNull String requestKey, @NonNull Bundle result) {
TextView tv2 = view.findViewById(R.id.tvHistory);
String hs = result.getString("hstry");
tv2.setText(hs);
}
});
return view;
}```