unknown
unknown

Reputation: 91

Send data from Adapter to broadcast receiver and receive in fragment

send data from to receiver and get in fragment

I want to receive data which I have passed from Adapter but it not even getting called

receiver class(the class is in Fragement)

 public class DataReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        try {
            if (intent != null) {
                Category category = (Category) intent.getSerializableExtra("category");
                Log.e("data", category.getName());
            }
        } catch (Exception e) {
            Log.e("catch", e.toString());

        }

    }
}

I have created receiver in below fragment and register and unregister receiver here.

AddServiceFragment class(receiver is in it)

  @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        bindViews();
        dataReceiver = new DataReceiver();
        filter = new IntentFilter("action");


    }
@Override
    public void onStart() {
        super.onStart();
        getActivity().registerReceiver(dataReceiver, filter);
    }


    public void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        getActivity().unregisterReceiver(dataReceiver);
    }

    @Override
    public void onResume() {
        // TODO Auto-generated method stub
        super.onResume();

        getActivity().registerReceiver(dataReceiver, filter);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        getActivity().unregisterReceiver(dataReceiver);

    }

I am using below code to send data to receiver

adapter code

 holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Intent in = new Intent(context, AddServiceFragment.DataReceiver.class);
            in.setAction("action");
            in.putExtra("category", category);
            context.sendBroadcast(in);
        }
    });

Upvotes: 0

Views: 2746

Answers (2)

Nischal
Nischal

Reputation: 880

Have a look at this: https://developer.android.com/training/basics/fragments/communicating.html You can communicate between your fragments through the host activity.

Often you will want one Fragment to communicate with another, for example to change the content based on a user event. All Fragment-to-Fragment communication is done through the associated Activity. Two Fragments should never communicate directly.

Upvotes: 0

Niranj Patel
Niranj Patel

Reputation: 33248

You are using wrong way. Please check below sample code.

Code in fragment class:

@Override
    public void onResume() {
        getActivity().registerReceiver(mReceiverLocation, new IntentFilter("data_action"));
        super.onResume();
    }

    @Override
    public void onPause() {
        getActivity().unregisterReceiver(mReceiverLocation);
        super.onPause();
    }

    private BroadcastReceiver mReceiverLocation = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            try {
                Category category = (Category) intent.getSerializableExtra("category");
                Log.e("data", category.getName());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };

Call from adapter class:

holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent in = new Intent("data_action");
                in.putExtra("category", category);
                context.sendBroadcast(in);
            }
        });

Upvotes: 1

Related Questions