Search code examples
androidandroid-lifecycleandroid-broadcastreceiverandroid-applicationinfo

Connectivity broadcastReceiver calls Application's onCreate method


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.


Solution

  • 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