Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I detect if the user has left my app?

Tags:

android

I am developing an Android app and I want to detect when the user exits my app either by clicking the Back button or the Home button.

Also, an event like onInit() would be useful in my scenario, as I just want to have the MyInıt action start at first.

onDestroy() is not called until other apps need more memory.

like image 807
enesness Avatar asked Jul 13 '11 17:07

enesness


People also ask

How do I know if an app is closed on Android?

The "onActivityDestroyed" will get called when the app is closed, so if you can check if the app is in background when it is called (so the app is already closed) you can grep exactly the moment when the app is being closed.

How do you know if an app is closed?

Currently there's no way to check an android app after force stopping it. If you want to use it, then you must revoke that app from its current state. Sadly there's no sandboxing available for Android OS.


1 Answers

Note: This only works if your target is >= API14


You can also use Application.registerActivityLifecycleCallbacks() and when any activity pauses post a delayed Runnable (that will invoke when user lefts app) to Handler. When activities are created/started/resumed you remove that Runnable from Handler. So when you navigate inside your app you always cancel that Runnable, but if another activity not from your app is activated - the Runnable will be invoked.

I used this to logout user when he lefts my app, here's code for callbacks:

public class MyApplication extends Application implements Application.ActivityLifecycleCallbacks {      private Handler handler;     private Runnable runLogout = new Runnable() {         @Override         public void run() {             //logoutUser()         }     };      @Override     public void onCreate() {         super.onCreate();          registerActivityLifecycleCallbacks(this);         handler = new Handler(getMainLooper());     }      @Override     public void onActivityCreated(Activity activity, Bundle savedInstanceState) {         handler.removeCallbacks(runLogout);     }      @Override     public void onActivityStarted(Activity activity) {         handler.removeCallbacks(runLogout);     }      @Override     public void onActivityResumed(Activity activity) {         handler.removeCallbacks(runLogout);     }      @Override     public void onActivityPaused(Activity activity) {         handler.postDelayed(runLogout, 1000);     }      @Override     public void onActivityStopped(Activity activity) {      }      @Override     public void onActivitySaveInstanceState(Activity activity, Bundle outState) {      }      @Override     public void onActivityDestroyed(Activity activity) {      } } 

However, Runnable will run not in the context of activity lifecycle, so to use this in activity you would need to set and check some application-wide flag or broadcast an intent.

like image 103
Justinas Saldukas Avatar answered Sep 22 '22 04:09

Justinas Saldukas