InterviewSolution
| 1. |
What is the difference between FindViewById and Data Binding? Which one is fast in performance? |
|
Answer» If you want to send data from fragment to another Fragment we need to use an interface. Intents are only usable for sending data on an Activity level. To pass data between fragments we need to CREATE our own interfaces. Fragment 1 -> MainActivity -> FragmentInterFace-> Fragment 2 In onAttach() lifecycle method , the fragment IMPLEMENT the Interface logic and it will call Interface methods to communicate with an activity. @Override PUBLIC void onAttach(Context context) { super.onAttach(context); if (context instanceof OnFragmentInteractionListener) { mListener = (OnFragmentInteractionListener) context; } else { throw NEW RuntimeException(context.toString() + " must implement OnFragmentInteractionListener"); } }The activity that hosts fragment must implement the interface For receiving callback events from Fragment class. First host activity creates an instance of fragment using findFragmentById method then it can deliver a MESSAGE to fragment directly by calling the fragment's public methods. |
|