[英]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) 接收器类(该类在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) AddServiceFragment类(其中有接收器)
@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);
}
});
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);
}
});
Have a look at this: https://developer.android.com/training/basics/fragments/communicating.html You can communicate between your fragments through the host activity. 看看以下内容: https : //developer.android.com/training/basics/fragments/communicating.html您可以通过主机活动在片段之间进行通信。
Often you will want one Fragment to communicate with another, for example to change the content based on a user event.
通常,您会希望一个Fragment与另一个Fragment进行通信,例如,根据用户事件更改内容。 All Fragment-to-Fragment communication is done through the associated Activity.
所有片段到片段的通信都是通过关联的活动完成的。 Two Fragments should never communicate directly.
两个片段永远不要直接通信。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.