我将 broadcastreceiver 从 Activity 发送到 fragment 。我的问题是 onReceive 不工作。
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup
container, Bundle savedInstanceState) {
IntentFilter(Constants.BroadCastMessages.UI_NEW_CHAT_ITEM));
onNotice = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
switch (action){
case Constants.BroadCastMessages.UI_NEW_CHAT_ITEM:
Log.d("RokayahBroadcast" , "from on receive");
}
}
};
public void onResume() {
super.onResume();
IntentFilter iff = new
IntentFilter(Constants.BroadCastMessages.UI_NEW_CHAT_ITEM);
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(onNotice , iff);
@Override
public void onPause() {
super.onPause();
getActivity().unregisterReceiver(onNotice);
请帮忙。 提前致谢
最佳答案
据我所知,BroadcastReceiver 不能注册到 Fragment 中,而是注册到 Activity 中:您可能应该在 Activity 级别注册此接收器,然后在收到后将其传递给您的 Fragment。
关于java - 广播接收器 onReceive 在 fragment 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53847703/