Name technique for passing data from fragment/acti

2019-03-03 16:46发布

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.

Can anybody point me to some resources on how to use that technique (Could it be called interface callbacks?)

All help is very appreciated!

3条回答
兄弟一词,经得起流年.
2楼-- · 2019-03-03 17:24

Here are steps:

  1. Download sample data from http://developer.android.com/training/basics/fragments/index.html(given in right side) and also look at url to how to add fragments from xml or dynamically to performing fragment transaction operations..

  2. Then would recommend you to go through with fragment guide..http://developer.android.com/guide/components/fragments.html

  3. Once you understand complete life cycle and its fragment callback methods then would be easy to understand example given by Google as sample.

  4. To defining interface in fragment to calling interface or passing callback to activity..

  5. Let’s say you have two fragments which shows list as article titles and article details.

  6. In your article list extends fragment list public class Fragment1 extends ListFragment
  7. Set your list view using list adapter in oncreateview method.

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
    android.R.layout.simple_list_item_1, Array);
    setListAdapter(adapter);
    
  8. Now we need to display article details when user click on article, so we need to pass position to activity to it can call back corresponding article details to show in fragment2.
  9. So when user click on article, system call onListItemClick callback method.

    public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    

    Call interface here and pass article position

  10. Define interface and pass position in method which activity will override.

    public interface OnArticleSelectedListener {
    public void onArticleSelected(int position);
    }
    
  11. In on attach method instantiates an instance of interface by casting the Activity, If the activity has not implemented the interface, then the fragment throws a ClassCastException. On success.

  12. Override interface method to display article details by passing position as bundle data to Fragment2.

Hope it will help you to understand sample code.

查看更多
叼着烟拽天下
3楼-- · 2019-03-03 17:37

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.

查看更多
啃猪蹄的小仙女
4楼-- · 2019-03-03 17:46

You can simple create new Android Application project in eclipse. Then create Android Object (Fragment) with callback methods. This will give you an idea for interfaces.

And then the same you can apply for activity to fragment.

查看更多
登录 后发表回答