Name technique for passing data from fragment/activity to fragment/activity with interfaces

后端 未结 3 787
庸人自扰
庸人自扰 2021-01-26 03:48

At school we\'re now learning on how to make fragments more universal by using interfaces. This technique is still kinda abstract and I don\'t really know when/how to use it.

3条回答
  •  攒了一身酷
    2021-01-26 04:20

    The callback approach, as you would call it, is as simple as Listener interface found in many parts of Java or Android. You may check the Observer pattern if you want to learn about a very general description. But if you already understand how to work with Listener, you will easily get the point about callbacks.

    NOTE: Do not mix it with Callback term - these are not the same.

    Suppose we have Activity MyActivity and Fragment MyFragment. We want to post some data from Fragment to Activity. Then let us create an interface within MyFragment:

    public class MyFragment extends Fragment{
    
        private PostDataCallback mCallback;//our Activity will implement this
    
        @Override
        public void onAttach(Activity activity) {
             super.onAttach(activity);
    
             // This makes sure that the container activity has implemented
             // the callback interface. If not, it throws an exception
             try {
                 mCallback = (PostDataCallback) activity;
             } catch (ClassCastException e) {
                 throw new ClassCastException(activity.toString()
                    + " must implement OnHeadlineSelectedListener");
             }
         }
    
    
        public interface PostDataCallback{
            public void onPostData(Object data);
        }
    
       /*
        we trigger this method when we calculated
             data or something like that and want to post it*/
    
        public void onSomeEvent(Object data){
    
            mCallback.onPostData(data);
        }
    }
    

    Our MyActivity will look like this:

    public class MyActivity extends Activity implements MyFragment.PostDataCallback{
    
        private Object data;
    
        @Override
        protected void onCreate(Bundle savedInstanceState){
            getFragmentManager().beginTransaction().add(R.id.some_container_id, new MyFragment(), "my fragment");
    
    
        }
    
        @Override
        public void onPostData(Object data){
            this.data = data;
            //some operations
    
        }
    
    }    
    

    So, MyFragment knows nothing about the implementation of it's callback. But it knows, that it can call the method onPostData(Object o) on the instance of PostDataCallback, which is held in the variable mCallback.

    Thus, when MyFragment triggers it's mCallback.onPostData(data), MyActivity get's the result.

    Exactly the same approach would work if we wanted to send message from MyActivity to MyFragment, but we would do it do it vice versa: the trigger method, callback interface definition and instance would reside in MyActivity, and MyFragment would implement the interface.

提交回复
热议问题