0

I just added a new blank fragment to my App and added 12 Checkboxes inside it. Now I need to read the isChecked property from another fragment. How I can do that.

I tried this but I get NullPointer exception when I try to use the variable.

View FiltersView = FilterEvents.newInstance().getView();//FilterEvents is another fragment

CheckBox F1_1 = FiltersView.findViewById(R.id.filter1_1);
CheckBox F1_2 = FiltersView.findViewById(R.id.filter1_2);
CheckBox F1_3 = FiltersView.findViewById(R.id.filter1_3);
CheckBox F1_4 = FiltersView.findViewById(R.id.filter1_4);
CheckBox F1_5 = FiltersView.findViewById(R.id.filter1_5);
CheckBox F1_6 = FiltersView.findViewById(R.id.filter1_6);
E_Blue
  • 1,021
  • 1
  • 20
  • 45
  • Possible duplicate of [Passing objects between fragments](https://stackoverflow.com/questions/42266436/passing-objects-between-fragments) – NerdyGinger Jul 09 '19 at 20:23
  • You can use an interface or something like EventBus [1]: https://docs.oracle.com/javase/tutorial/java/concepts/interface.html [2]: https://github.com/greenrobot/EventBus – ASHKARAN Jul 09 '19 at 20:27
  • 1
    You can use ViewModel objects to share data between fragments. ViewModel would be linked to activity that owns fragments – Gregory Buiko Jul 09 '19 at 20:37
  • ViewModel or with the supportFragmentManager(accessible by getActivity() in Fragment), you can get the list of All Fragments and then find and get a reference of the Fragment where the CheckBox is and then call Fragment.getView() – User One Jul 09 '19 at 20:58
  • Also, there is not guarantee that the Fragment View will be ready when you call getView() this way just after newInstance() – User One Jul 09 '19 at 21:08

0 Answers0