3

I have a BroadcastReceiver which receives broadcast sent to a Fragment. I'm getting the broadcast but how can I call a method from the Fragment itself? I basically need to update a List once the broadcast arrives, the List & update method are part of the Fragment.

public class FragmentReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if(action != null && action.equals("AllTasksFragmentUpdate"))
        {
            //
        }
    }
}

Registering the receiver:

    @Override
public void onCreate(final Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    getActivity().registerReceiver(new FragmentReceiver(), new IntentFilter("AllTasksFragmentUpdate"));
}

How can I call the method from the Fragment?

David Faizulaev
  • 4,651
  • 21
  • 74
  • 124
  • who is implementing this broadcast receiver if fragment then you can call method easily inside brodacast receiver. – dex Mar 14 '16 at 20:04
  • You can register a receiver inside your fragment and implement there only and then you can call any of your fragment's methods. – Shadab Ansari Mar 14 '16 at 20:09
  • You can simply call the Fragment's method from within the broadcast receiver's onReceive method. Can't you? – Ruchira Randana Mar 14 '16 at 20:19
  • @ShadabAnsari, I've edited the original question, posted the register receiver call. – David Faizulaev Mar 14 '16 at 20:23
  • @dex, I tried but it cannot find it. I've edited the original question, posted the register receiver call. – David Faizulaev Mar 14 '16 at 20:24
  • @DavidFaiz do one think instead of extending your class to broadcast receiver , register your broadcast receiver inside your fragment and then you can easily call your methods, Please let me you know if you require sample code for the same. – dex Mar 15 '16 at 03:59
  • @dex, sample code would be great!, I'm just super new to this Android stuff. – David Faizulaev Mar 15 '16 at 06:27
  • 1
    @DavidFaiz I have given you the sample code to implement broadcast receiver. – dex Mar 15 '16 at 07:01

4 Answers4

11

You can implement your broadcast reciever in the following way:

import android.app.Fragment;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.LocalBroadcastManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.adobe.libs.connectors.R;

public class YourFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater,
                                @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        //Start listening for refresh local file list   LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mYourBroadcastReceiver,
                new IntentFilter(<YOUR INTENT FILTER>));

        return inflater.inflate(R.layout.your_fragment_layout, null, true);
    }

    @Override
    public void onDestroyView() {
        LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(mYourBroadcastReceiver);
    }

    private final BroadcastReceiver mYourBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // Now you can call all your fragments method here
        }
    };
}
David C.
  • 1,974
  • 2
  • 19
  • 29
dex
  • 5,182
  • 1
  • 23
  • 41
2

In your onReceive() method, you can find your fragment by its tag name (the name using which you instantiated your fragment) and call its public method.

SampleFragment fragment = (SampleFragment) getSupportFragmentManager().findFragmentByTag(<fragment_tag_name>);
                if (fragment != null && fragment.isAdded()) {
                    fragment.method(); //Call any public method
                }

Hope this helps !

Shadab Ansari
  • 7,022
  • 2
  • 27
  • 45
  • I get an error 'cannot resolve getSupportFragmentManager'. I believe the Receiver class needs to extend 'FragmentActivity' but it already extends 'BroadcastReceiver'. – David Faizulaev Mar 14 '16 at 20:52
  • The activity containing your receiver and/or fragment should extend FragmentActivity. – Shadab Ansari Mar 14 '16 at 21:16
  • getSupportFragmentManager is not an available method in BroadcastReceiver. Your code doesn't work if the BroadcastReceiver is a standalone class. – gyleg5 Jan 20 '21 at 22:56
2

Supposing that you have an Activity named MyActivity and at some point in your code, you want to make a broadcast to be received by a Fragment named MyFragment. You can do it like this:

In MyActivity, use LocalBroadcastManager to make the broadcast with a filter:

String filter = "thisIsForMyFragment";
Intent intent = new Intent(filter); //If you need extra, add: intent.putExtra("extra","something");
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);

In MyFragment, inside onCreateView() add this to register the receiver:

LocalBroadcastManager.getInstance(getActivity()).registerReceiver(myBroadcastReceiver,
        new IntentFilter("thisIsForMyFragment"));

Following, In MyFragment (outside onCreateView()) create the receiver method to receive the broadcast:

private final BroadcastReceiver myBroadcastReceiver = new BroadcastReceiver()
{
    @Override
    public void onReceive(Context context, android.content.Intent intent) {
        Toast.makeText(getActivity(), "Broadcast received!", Toast.LENGTH_SHORT).show();//Do what you want when the broadcast is received...

    }
};

Finally, in MyFragment add this to unregister the receiver onDestroyView:

@Override
public void onDestroyView()
{
    super.onDestroyView();
    LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(myBroadcastReceiver);
}
0
activity!!.registerReceiver(receiver, IntentFilter(DataKeys.STATUS))
Dharman
  • 30,962
  • 25
  • 85
  • 135
Gurwinder Singh
  • 109
  • 1
  • 3