ldd379077640
聆听-花语
2017-02-22 10:42
采纳率: 34.8%
浏览 2.7k

android fragment广播注销问题

从A类跳转到B类,B类有个发送广播的按钮!第一次发送广播,在fragment中接收并执行b操作!当我退到A类,再跳转B类后,再点击按钮发送那个广播,在fragment中会执行两次b操作!我退三次,就会执行三次b操作!
这是怎么回事呢?
代码如下:
B类中发送广播:
Intent inten = new Intent("android.intent.action.Fa");
LocalBroadcastManager.getInstance(CommodityActivity.this).sendBroadcast(inten);

侧滑fragment接收广播:
LocalBroadcastManager broadcastManager2 = LocalBroadcastManager.getInstance(getActivity());
IntentFilter intentFilter2 = new IntentFilter();
intentFilter2.addAction("android.intent.action.guafff");
BroadcastReceiver mItemViewListClickReceiver2 = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent inten) {
b操作
}
};
broadcastManager2.registerReceiver(mItemViewListClickReceiver2, intentFilter2);

就这么多,我还该怎么改过来?求大神解答,甚急!甚急!!
无悬赏金币,可加QQ:379077640!需要的话,必有感谢!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • ldd379077640
    聆听-花语 2017-02-23 01:50
    已采纳

    已解决!fragment中添加:
    @Override
    public void onDestroyView() {
    super.onDestroyView();
    broadcastManager2.unregisterReceiver(mItemViewListClickReceiver2);
    }

    点赞 评论
  • ziyejinwei1994
    ziyejinwei1994 2017-02-22 11:26

    退到B类的时候能不能杀死B类

    点赞 评论
  • FengShanChuiChui
    风扇吹吹 2017-02-22 12:42

    BroadcastReceiver mItemViewListClickReceiver2 = new BroadcastReceiver()

    你的mItemViewListClickReceiver2是个局部变量?会不会重复注册?

    点赞 评论

相关推荐