1

I need to make status of user offline. When I press home button onStop() is called, that's fine. When I press back button onDestroy() is invoked. But when I close the app from recent apps by swiping it, onStop() or onDestroy() isn't called.

I need to know when the app is closed from recent apps to do something (e.g make user offline).

Cœur
  • 37,241
  • 25
  • 195
  • 267
Muhammad Jamal
  • 442
  • 4
  • 21

3 Answers3

3
  1. Make a service :

    public class MyService extends Service {
    private DefaultBinder mBinder;
    private AlarmManager  alarmManager ;
    private PendingIntent alarmIntent;
    
    private void setAlarmIntent(PendingIntent alarmIntent){
    this.alarmIntent=alarmIntent;
    }
    
    public void onCreate() {
    alarmManager (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    mBinder = new DefaultBinder(this);
    }
    
     @Override
     public IBinder onBind(Intent intent) {
      return mBinder;
     }
    
     public void onTaskRemoved (Intent rootIntent){
     alarmManager.cancel(alarmIntent);
     this.stopSelf();
    }
    }
    
  2. Make a custom class :

    public class DefaultBinder extends Binder {
        MyService s;
    
        public DefaultBinder( MyService s) {
            this.s = s;
        }
    
        public MyService getService() {
            return s;
        }
    }
    
  3. Add to your activity :

    MyService service;
    protected ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder binder) {
    service = ((DefaultBinder) binder).getService();
    service.setAlarmIntent(pIntent);
        }
    
        public void onServiceDisconnected(ComponentName className) {
            service = null;
        }
            };
    
    protected void onResume() {
        super.onResume();
        bindService(new Intent(this, MainService.class), mConnection,
                Context.BIND_AUTO_CREATE);
    }
    
    @Override
    protected void onStop() {
        super.onStop();
    
        if (mConnection != null) {
            try {
                unbindService(mConnection);
            } catch (Exception e) {}
        }
    }
    
1

But when I close the app from recent apps by swiping it, onStop() or onDestroy() isn't called.

Methods of Activity lifecycle that are to be called when Activity is no longer visible are not guaranteed to be called when removed from the recent tasks (treat it as "soft" version of killing an app by the system due to low memory).

I need to know when the app is closed from recent apps to do something (e.g make user offline)

I suggest one of the following:

  • (If applicable) use Activity's onResume()/onPause() to "make user online/offline";
  • use Service that sticks to the application meaning that if the app is killed after Service's onStartCommand() returns, the service will be recreated and onStartCommand() will be called again. At this point you could "make user offline". The chain of lifecycle method calls would be:

    1. Activity's onStop() -> onDestroy()* ->
    2. Service's onTaskRemoved()* ->
    3. Application's onCreate() -> Service's onCreate() ->
    4. Service's onStartCommand()

The Intent passed to the method will help you recognize which component triggered the start request:

  • Intent != null, meaning the request has been received from a running Activity instance
  • Intent = null, meaning the request has been sent by the (newly created) Application instance

* Not guaranteed to be called

Onik
  • 19,396
  • 14
  • 68
  • 91
  • No @Onik, you can't even use a sticky service because this service will be brutally terminates by the Android OS – Attiq ur Rehman Mar 01 '16 at 18:07
  • @Attiq ur Rehman ...and after that the service will be recreated. See [START_STICKY](http://developer.android.com/reference/android/app/Service.html#START_STICKY). – Onik Mar 01 '16 at 18:37
0

No, there is no clean way to get the application termination time. But I could suggest you a dirty trick to use a service to update your application (offline functionalities) for every after n minutes.

When OS kill your application, it will remove all associated services and broadcast receiver along with it.

Attiq ur Rehman
  • 475
  • 1
  • 6
  • 21