Basically I've got an application that runs a foreground service.
When I start the application I need to do session specific initialization in the Application's onCreate
method.
When I close the app, the service keeps running (desired behaviour), however, when I reopen the app from the launcher / from my notification, the Application onCreate
isn't getting called again.
My question are:
- How do I enforce the Application
onCreate
be called again even though there's a service running? (The service is probably keeping a reference to the application object, right?) - Is there a way to get indication inside the Application class that the app has been started again but from a service?
- What other solutions can you think about?
MyService in AndroidManifest.xml
:
<service android:name=".MyService"
android:exported="false"/>
My service's onStartCommand
:
createNotificationChannel();
Intent notificationIntent = new Intent(this, MyActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("My Service")
.setContentText("Service")
.setSmallIcon(R.drawable.ic_android)
.setContentIntent(pendingIntent)
.build();
startForeground(1, notification);