HMK
HMK

Reputation: 393

Listen to same result from different Android Fragments

There are 2 Fragments I'm calling a service from Fragment 1. I have a ResultReceiver in Fragment 1 which listens to the result and onReceiveResult will call method1().

I want a ResultReceiver in Fragment 2 to listen to the same response but onReceiveResult will be calling method2()

How can I achieve this?

Upvotes: 0

Views: 936

Answers (3)

j2emanue
j2emanue

Reputation: 62549

just pass into your service two different ResultReceiver's ... If the service is already started calling startService(Intent) again just makes you call onStartCommand(...) and then you can set your resultReciever each time. So you can keep an array of resultreciever's if you want.

saying that, i would never do it this way. Research Java Observer pattern. Java has a default implementation of the Observer pattern. Here is a link

Upvotes: 1

Janus Varmarken
Janus Varmarken

Reputation: 2336

You could specify an interface:

interface Receiver {
    void onResult();
}

Have your two Fragments implement this interface. Fragment1's implementation simply calls method1(), and Fragment2's implementation simply calls method2():

public class Fragment1 extends Fragment implements Receiver {
    // Remember to register and remove the receiver (e.g. in onAttach and onDetach respectively).
    private MyReceiver mBroadcast = new MyReceiver(this);
    public void onResult() {
        this.method1();
    }
}

public class Fragment2 extends Fragment implements Receiver {
    // Remember to register and remove the receiver (e.g. in onAttach and onDetach respectively).
    private MyReceiver mBroadcast = new MyReceiver(this);
    public void onResult() {
        this.method2();
    }
}

Then specify the BroadcastReceiver as a standalone (or inner static) class such that both Fragment1 and Fragment2 will be able to instantiate it:

public class MyReceiver extends BroadcastReceiver {
    private Receiver mFragment;
    public MyReceiver(Receiver fragment) {
        mFragment = fragment;
    }

    public void onReceive(Context context, Intent intent) {
        if(intent.getAction().equals(YOUR_ACTION) {
            mFragment.onResult();
        }
    }
}

Upvotes: 1

JIthin
JIthin

Reputation: 1453

I don't think that you can receive results in two different fragments simultaneously. But there are many ways to achieve this.. 1. I believe the easiest way will be to use object reference.. There are two possibilities.. Either create a static method in Fragment 2 and call it from fragment 1 from onReceiveResult(). Or Create an object of Fragment 2 in fragment 1 and from fragment 2 , assign that it is the same as the instance created by fragment1. Then just call object_of_frgament2.method2() from the onReceiveResult() of fragment 1.

2. Using interface.Create a custom interface and make the Fragment 2 implement the interface and create an instance of the interface in Fragment 1. and within onReceiveResult() of Fragment1 you can call the interface method.

While implementing the interface, you can get the result in fragment 2 in the interface method. Just call method2() from the function....

3.Using Broadcast Receiver.. Create a custom broadcast receiver and make all the fragments/activities which need the results to listen to it. and within onReceiveResult() of Fragment1 just broadcast the result..

I believe there are still other ways to do it..

Upvotes: 1

Related Questions