title: Activity has leaked IntentReceiver Error
body: <p>before marking this post as closed or duplicate i want to say that i have done all the things that are mentioned on similar posts but none of them worked.</p>
  
  <p>I have 2 receivers which i get data from. So i want to register the receivers when the activity starts and unregister them when the activity is not visible.</p>
  
  <p>My Code:</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
      private CheckNetworkStatusReceiver checkNetworkStatusReceiver;
      private CheckBatteryStatusReceiver checkBatteryStatusReceiver;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          checkConnection();
      }
      public void checkConnection() {
          if(!ConnectionManager.isNetworkAvailable(MainActivity.this)){
             ConnectionManager.wifiSettingsDialog(MainActivity.this).show();
          }else{
              registerReceivers();
          }
      }
  
      public void registerReceivers() {
          checkNetworkStatusReceiver = new CheckNetworkStatusReceiver();
          checkBatteryStatusReceiver = new CheckBatteryStatusReceiver();
          registerReceiver(checkNetworkStatusReceiver, new IntentFilter(Constants.INTENT_FILTER_CONNECTIVITY_CHANGE));
          registerReceiver(checkBatteryStatusReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
      }
  
  
      @Override
      protected void onDestroy() {
          super.onDestroy();
          try{
              unregisterReceiver(checkNetworkStatusReceiver);
              unregisterReceiver(checkBatteryStatusReceiver);
          }catch (Exception e){
              e.printStackTrace();
          }
  
      }
  
  
      @Override
      protected void onResume() {
          registerReceivers();
          super.onResume();
      }
  
      @Override
      protected void onRestart() {
          registerReceivers();
          super.onRestart();
      }
  
      @Override
      protected void onStop() {
          super.onStop();
  
          try{
              unregisterReceiver(checkNetworkStatusReceiver);
              unregisterReceiver(checkBatteryStatusReceiver);
          }catch (Exception e){
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>The receiver code:</p>
  
  <pre><code>public class CheckNetworkStatusReceiver extends BroadcastReceiver{
  
      @Override
      public void onReceive(Context context, Intent intent) {
          if (intent != null){
              Log.e("Action ",intent.getAction());
              if(intent.getAction().equalsIgnoreCase(Constants.INTENT_FILTER_CONNECTIVITY_CHANGE)) {
                  if (!ConnectionManager.isNetworkAvailable(context)){
                      Toast.makeText(context, R.string.no_internet, Toast.LENGTH_SHORT).show();
                  }
                  if (MobileDataManager.slowInternetConnection(context)){
                      Toast.makeText(context, R.string.slow_internet_delay, Toast.LENGTH_SHORT).show();
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>and the battery receiver:</p>
  
  <pre><code>public class CheckBatteryStatusReceiver extends BroadcastReceiver {
      @Override
      public void onReceive(Context context, Intent intent) {
          if (intent != null){
              int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
              if (level &lt;= 15){
                  Toast.makeText(context, "Batter level " + level + "% is very low. Please connect to a charger.", Toast.LENGTH_SHORT).show();
              }
          }
      }
  }
  </code></pre>
  
  <p>i get the error on the <code>registerReceivers()</code> method. Any ideas?</p>