Home » Android » android – How to registerReceiver in Fragment

android – How to registerReceiver in Fragment

Posted by: admin May 14, 2020 Leave a comment

Questions:

I show Bluetooth devices in a ListView. I tried it in an Activity before and it worked, but now I have to include this ListView in a Fragment

It´s clear that one or two things don´t belong here, like the registerReceiver, unregisterReceiver and RESULT_CANCELED.

protected void onResume() {
    registerReceiver(receiver, filter);
     filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
    registerReceiver(receiver, filter);
     filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    registerReceiver(receiver, filter);
     filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
    registerReceiver(receiver, filter);
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    unregisterReceiver(receiver);


}

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == RESULT_CANCELED){
            Toast.makeText(getActivity()     , "El Bluetooth debe estar activado para continuar", Toast.LENGTH_SHORT).show();
            getActivity().finish();
        }
    }

The lines that start with registerReceiver, unegisterReceiver and if(resultCode==RESULT_CANCELED){ are giving me problems.

As an aditional fact, I have tried to to change it to an Activity instead of a Fragment, but my Main class have an error with that because I am working with Fragments.

How to&Answers:

All these methods and variables belong to Activity class. So consider calling them with context of parent Activity of Fragment.

You may call required methods as:

 getActivity().registerReceiver(receiver, filter);

and

 getActivity().unregisterReceiver(receiver);

if(resultCode==RESULT_CANCELED){

can be replaced with

if(resultCode == Activity.RESULT_CANCELED){

Hope this solve your problem.

Answer:

Regarding to a broadcast message, you won’t receive anything if you register like that:

getActivity().registerReceiver(receiver, filter);

You should write either:

LocalBroadcastManager.getInstance(getActivity()).registerReceiver(receiver, filter);

For instance, case of an intent sent from a service (IntentService).

Answer:

The registerReceiver() and unregisterReceiver() methods are methods of the Context class. In this case, you need your Activity (which is a Context).

Fragments provide easy access to their containing Activity via the getActivity() method.

RESULT_CANCELED is simply a static variable on the Activity class, so you can access it anywhere via Activity.RESULT_CANCELED.

Answer:

given that activity is recreated in configuration changes while fragment resist, it means activity context might disappear which leads to losing receivers in activity recreation. So I’d suggest to register broadcast receivers in your fragment with application level context. You then may register and unregister your receivers in onResume() and onPause() of the fragment respectively. This way your receivers are bound to the fragment lifecycle not the activity.