1

I have an activity which calls another fragmentA .Now this fragmentA calls another fragmentB .Now I want to transfer data from fragmentB to my activity

Shakeeb Ayaz
  • 6,200
  • 6
  • 45
  • 64

4 Answers4

2

check this: Communicating with Other Fragments

Define an Interface (In fragment)

To allow a Fragment to communicate up to its Activity, you can define an interface in the Fragment class and implement it within the Activity. The Fragment captures the interface implementation during its onAttach() lifecycle method and can then call the Interface methods in order to communicate with the Activity.

Here is an example of Fragment to Activity communication:

public class HeadlinesFragment extends ListFragment {
    OnHeadlineSelectedListener mCallback;

    // Container Activity must implement this interface
    public interface OnHeadlineSelectedListener {
        public void onArticleSelected(int position);
    }

    @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 = (OnHeadlineSelectedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnHeadlineSelectedListener");
        }
    }

    ...
}

Now the fragment can deliver messages to the activity by calling the onArticleSelected() method (or other methods in the interface) using the mCallback instance of the OnHeadlineSelectedListener interface.

For example, the following method in the fragment is called when the user clicks on a list item. The fragment uses the callback interface to deliver the event to the parent activity.

@Override
public void onListItemClick(ListView l, View v, int position, long id)   {
    // Send the event to the host activity
    mCallback.onArticleSelected(position);
}

Implement the Interface (in activity)

In order to receive event callbacks from the fragment, the activity that hosts it must implement the interface defined in the fragment class.

For example, the following activity implements the interface from the above example.

public static class MainActivity extends Activity
        implements HeadlinesFragment.OnHeadlineSelectedListener{
    ...

    public void onArticleSelected(int position) {
        // The user selected the headline of an article from the HeadlinesFragment
        // Do something here to display that article
    }
}

PS1: EventBus works for you, but use it carefully if you need, it may make your code harder to read.

PS2: Don't pass an activity instance in Fragment.newInstance() and communicate using it. The activity instance may be destroyed in background. Get activity instance in Fragment.onAttach() like the example, the framework will handle the destroy & recreate & rebind for you.

Alex.Li
  • 540
  • 1
  • 4
  • 12
1

You can get the Activity instance by overriding onAttach() method

Method: 1

@Override
public void onAttach(Activity activity) {
    // TODO Auto-generated method stub
    super.onAttach(activity);
    this.activity=(ActivityName) activity;
}

and

    activity.setdata(yourdata);

or

Method : 2

((ActivityName)getActivity()).setdata(yourdata);

In these ways you need to create a setter method in your activity

It is easiest way to get callback from any fragment to its parent activity .Very well explained by Dev Doc here

Shakeeb Ayaz
  • 6,200
  • 6
  • 45
  • 64
uday
  • 1,348
  • 12
  • 27
0

You can get the instance of the activity by calling getActivity() in fragmentB and pass the data using an interface.

null pointer
  • 5,874
  • 4
  • 36
  • 66
0

You could use EventBus if you think that, you will need a similar functionality in different parts of the app and you don't want to write many interfaces for this porpoise. You could use https://github.com/greenrobot/EventBus

Example:

Add compile 'de.greenrobot:eventbus:2.4.0', Register on activities OnCreate -

EventBus.getDefault().register(this);

and OnDestroy-

EventBus.getDefault().unregister(this);

Than add a method with a receiving object parameter and with onEventMainThread name:

public void onEventMainThread(YourObject name) {...}

Now from any Fragament you can call

EventBus.getDefault().post(yourObjectInstance);

And activity will detect it.

Or you could use RxJava to get similar effect - http://nerds.weddingpartyapp.com/tech/2014/12/24/implementing-an-event-bus-with-rxjava-rxbus/

Mikelis Kaneps
  • 4,576
  • 2
  • 34
  • 48