title: How to unregisterReceiver which is used in fragment?
body: <p>I have onReceive method implemented in my FragmentInfo class, because I need to update UI, when intent is send from somewhere else in my code. I register this receiver from Fragment, but from MainActivity, because it must be registered from Activity. I don't know where to place unregisterReceiver call, so my program crashes. I'm getting error:</p>
  
  <p><strong>MainActivity has leaked IntentReceiver FragmentInfo that was originally registered here. Are you missing a call to unregisterReceiver()?</strong></p>
  
  <p>I was trying to call unregisterReceiver from MainActivity onPause method, like it is usually done, but from MainActivity I can't accessed mBroadcastReceiver. How can I solve this?</p>
  
  <pre><code>public class FragmentInfo extends Fragment {
  
      //nested class BroadcastReceiver
      private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
  
          //from there we must update FragmentInfo UI
          @Override
          public void onReceive(Context context, Intent intent) {
              //updateFragmentInfo(intent);
              updateUiInfo();
              //unregisterReceiver
          ((MainActivity)getActivity()).unregisterReceiver(mBroadcastReceiver);
          }
      };
  
      private TextView textView3;
      private TextView textView5;
      private TextView textView7;
      private TextView textView8;
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {     
          //dynamic register BroadcastReceiver, for triggering intent when BT connection is established
          //BroadcastReceiver can be registered static in ManifestFile
          IntentFilter mIntentFilter = new IntentFilter();
          //register message 
          mIntentFilter.addAction("bluetooth_established_intent");
          //we must call registerReceiver from MainActivity!
          ((MainActivity)getActivity()).registerReceiver(this.mBroadcastReceiver,mIntentFilter);
      }
  }
  </code></pre>
  
  <p><strong>EDIT: I unregister receiver after call updateUIInfo and it works now. I edit in code.</strong></p>