#StackBounty: #android #android-service #android-lifecycle #android-service-binding #android-application-class How do I enforce Applica…

Bounty: 50

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:

  1. 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?)
  2. Is there a way to get indication inside the Application class that the app has been started again but from a service?
  3. 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);


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.