0

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.

narb
  • 958
  • 1
  • 13
  • 39

1 Answers1

1

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.

theJango
  • 1,100
  • 10
  • 22
  • let's make sure I do it right. I have my simple program running that does a Log.i inside onDestroy. Then I increment the version code and do your adb command. My running app is stopped and replaced by the new version. But I don't get my Log,i. This means to me the life cycle is not respected in the upgrade case? – narb Jul 28 '18 at 18:59
  • Actually onDestroy() & onStop() are never guaranteed to get called in lifecycle. In this case, Android actually kills the host process itself and restart the newly installed application. – theJango Jul 28 '18 at 19:03
  • not cool ;) do you see another way to check if the process is still running? – narb Jul 28 '18 at 19:12
  • Can I ask the requirement for this. Ideally You should not relying on destroy of activity or application. If You can explain the requirement their might be some other easy way to implement that. – theJango Jul 28 '18 at 19:14
  • a long story. I have a widget that uses a broadcast receiver (and other stuff) but in case the home app exits it stops the BR. At least if I rely on onDestroy I can notify the user. It works in 99% of the time except the upgrade. – narb Jul 28 '18 at 19:20
  • I'll try tomorrow. Thanks for the help! :) – narb Jul 28 '18 at 21:21
  • Nice try but it doesn't work either. onDestroy or onTaskRemoved are not called on adb install. Thanks for the adb tip! – narb Jul 29 '18 at 04:12
  • Then it won't be possible I guess. System kills the process directly. – theJango Jul 29 '18 at 04:31
  • I just did the test on my app and it works fine through the adb cmd :) So maybe you don't get the Log msg but it calls onDestroy! cool! Thanks again :) – narb Jul 29 '18 at 04:44
  • So as long as you remember that studio is the exception you're fine. – narb Jul 29 '18 at 04:46