0

In android lifecycle, is there any unique method that gets called when the app is swiped from the recent app list? We initially thought we would use onDestroy but it gets called during other activities as well.

1 Answers1

0

Add a service like below and start it. When app close onTaskRemoved run your code.

Manifest

<service
    android:enabled="true"
    android:name=".ExitService"
    android:exported="false"
    android:stopWithTask="false" />

Service class

public class ExitService extends Service {

@Override
public void onStartService() {}

@Override
public void onTaskRemoved(Intent rootIntent) {
    super.onTaskRemoved(rootIntent);
    this.stopSelf();
}}
Hamid-Ghasemi
  • 275
  • 2
  • 6
  • I've added the service and class in my project. But onTaskRemoved was not called when I removed app from recent apps. Any other pointers which I may be missing. – Tanmay Joshi Jun 09 '22 at 18:01
  • Thanks, this did not helped directly but got hint and it helped to resolve the issue. – Tanmay Joshi Jun 10 '22 at 15:38