onDestroy with an update from the store or a reload from the studio

160 Views Asked by At

In my application I need to detect when the user ends the app. When I do a reload from Android Studio what I see is that the app does not go through onDestroy.

I presume this is because the studio uses instant run? It's not an issue as long as it is limited to the dev environment.

But what happens when the user updates the app from the store? Am I sure that the app goes to onDestroy before being re-opened?

I'm not sure how exactly to test it so I thought I could ask the question from the forum.

1

There are 1 best solutions below

10
On BEST ANSWER

Create an APK using your declared targets like debug or release. Install the APK in your device. Now launch your application. While application was being in the foreground. Install APK again using the following command.

adb install -r PATH_TO_TOUR_APK

This similar to app upgrade from Play Store. Just make sure both APK have same signing certificate and an increased versionCode.

Upadte Answer: If You want to notify user when App gets killed Please try following:

public class EmptyService extends Service {

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.d("EmptyService", "Service Started");
    return START_NOT_STICKY;
}

@Override
public void onDestroy() {
    super.onDestroy();
    Log.d("EmptyService", "Service Destroyed");
}

@Override
public void onTaskRemoved(Intent rootIntent) {
    Log.e("EmptyService", "END");
    //Code here
    stopSelf();
}

}

In the AndroidManifest

<service android:name="com.example.EmptyService" android:stopWithTask="false" />

Now start this service from start of your applicatoin.

Let me know if it works in case of upgrade.