1

On my application, I'm doing a set up (including calls to our server) on Application's onCreate method. On the other hand, we use a broadcastReceiver to get the changes on connectivity. Everything seems to be working fine but, if we kill the application and then there's a change in connectivity then the Application's onCreate method is called, which causes a call to our server that shouldn't be done. Is this the normal behavior? How can we avoid the onCreate method to be called on connectivity change?

The connectivity broadcast receiver is configured this way in the AndroidManifest:

<receiver android:name="x.ConnectivityReceiver" android:label="ConnectivityReceiver">
    <intent-filter>
        <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
    </intent-filter>
</receiver>

Thanks in advance.

FVod
  • 2,245
  • 5
  • 25
  • 52

1 Answers1

1

This is normal behavior, because your application is started (created) for your BroadcastReceiver. The only way to prevent onCreate from being called is removing your BroadcastReceiver.
You could register your receiver dynamically in your Application class or an Activity, instead of in the manifest. I don't know if that suits your needs though

0xDEADC0DE
  • 2,453
  • 1
  • 17
  • 22
  • Thank you for your answer. Then I guess that I will have to register the broadcast in my application class. But then, it will never be unregistered, would it be a problem? – FVod Apr 01 '16 at 07:49
  • You can unregister it in `onTerminate` or at some other point. If you do not unregister it and your applications is finished, you will see a stacktrace in logcat saying that the receiver leaked. However, I believe it is not a fatal exception – 0xDEADC0DE Apr 01 '16 at 08:03