Skip to content Skip to sidebar Skip to footer

Name Technique For Passing Data From Fragment/activity To Fragment/activity With Interfaces

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 any

Solution 1:

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 ActivityMyActivity and Fragment MyFragment. We want to post some data from Fragment to Activity. Then let us create an interface within MyFragment:

publicclassMyFragmentextendsFragment{

    privatePostDataCallback mCallback;//our Activity will implement this@OverridepublicvoidonAttach(Activity activity) {
         super.onAttach(activity);

         // This makes sure that the container activity has implemented// the callback interface. If not, it throws an exceptiontry {
             mCallback = (PostDataCallback) activity;
         } catch (ClassCastException e) {
             thrownewClassCastException(activity.toString()
                + " must implement OnHeadlineSelectedListener");
         }
     }


    publicinterfacePostDataCallback{
        publicvoidonPostData(Object data);
    }

   /*
    we trigger this method when we calculated
         data or something like that and want to post it*/publicvoidonSomeEvent(Object data){

        mCallback.onPostData(data);
    }
}

Our MyActivity will look like this:

publicclassMyActivityextendsActivityimplementsMyFragment.PostDataCallback{

    privateObject data;

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState){
        getFragmentManager().beginTransaction().add(R.id.some_container_id, newMyFragment(), "my fragment");


    }

    @OverridepublicvoidonPostData(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.

Solution 2:

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 = newArrayAdapter<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.

    publicvoidonListItemClick(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.

    publicinterfaceOnArticleSelectedListener {
    publicvoidonArticleSelected(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.

Solution 3:

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.

Post a Comment for "Name Technique For Passing Data From Fragment/activity To Fragment/activity With Interfaces"