The onAttach() method is called as soon as the Fragment is associated with the Activity. The code makes sure that the host Activity has implemented the callback interface. If not, it throws an exception.
The string resource exception_message is included in the source code for the text "must implement OnFragmentInteractionListener"
Overall, your class should end up looking like this.
publicclassRatingFragmentextendsFragment {privatestaticfinalString CHOICE="choice";publicfloat choice;OnFragmentInteractionListener mListener;interfaceOnFragmentInteractionListener {voidonRatingChoice(float choice); }publicRatingFragment() {// Required empty public constructor }publicstaticRatingFragmentnewInstance(float choice) {RatingFragment fragment =newRatingFragment();Bundle arguments =newBundle();arguments.putFloat(CHOICE, choice);fragment.setArguments(arguments);return fragment; } @OverridepublicViewonCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {finalView rootView =inflater.inflate(R.layout.fragment_rating, container,false);finalRatingBar rB =rootView.findViewById(R.id.ratingBar);if (getArguments().containsKey(CHOICE)) {// A choice was made, so get the choice. choice =getArguments().getFloat(CHOICE);// Check the radio button choice.if (choice!=0) {rB.setRating(choice); } }rB.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() { @OverridepublicvoidonRatingChanged(RatingBar ratingBar,float v,boolean b) {String s =getString(R.string.rating_message); choice = v;mListener.onRatingChoice(v);//Toast.makeText(getActivity().getApplicationContext(),s+ " "+ v,Toast.LENGTH_SHORT).show(); } });return rootView; } @OverridepublicvoidonAttach(Context context) { super.onAttach(context);if (context instanceof OnFragmentInteractionListener) { mListener = (OnFragmentInteractionListener) context; } else {thrownewClassCastException(context.toString()+ getResources().getString(R.string.exception_message)); } }}
Implementing OnFragmentInteractionListener
Now that we have set up our RatingListener for communication. We need to implement the OnFragmentInteractionListener in the activity in order to receive data from fragment
The app should now show a toast when you click it.
Providing user's choice to fragment (Main
We have to now set a Bundle and use the Fragment.setArguments(Bundle) method to supply the construction arguments for the Fragment.
A Bundle is like a key-value dictionary which allows to transfer information to other activities, fragments.
First, let's setup a newInstance() method to use a Bundle and setArguments
publicstaticRatingFragmentnewInstance(float choice) {RatingFragment fragment =newRatingFragment();Bundle arguments =newBundle();arguments.putFloat(CHOICE, choice);fragment.setArguments(arguments);return fragment; }
Add following code to onCreateView() method to retrieve the previously set value of choice
if (getArguments().containsKey(CHOICE)) {// A choice was made, so get the choice. choice =getArguments().getFloat(CHOICE);// Check the radio button choice.if (choice!=0) {rB.setRating(choice); } }
Lastly, we need to change our functions in MainActivity to RatingFragment.newInstance(mRatingChoice);
Your app should be communicating the choice from the Fragment to the Activity, and then back to the Fragmentwhich should display the choice you made previously always