0

I know there is onPause() and onDestroy() method. I have an app with so many activities and fragments. I was wondering if there any way I can add log "App Exited", whenever user leaves the app from any activity or fragment?

2 Answers2

0
  1. Create an application class and use OnLifecycleEvent:

     import androidx.lifecycle.OnLifecycleEvent
    
     class MyApplication: Application() {
    
         @OnLifecycleEvent(Lifecycle.Event.ON_START)
         fun appInForeground() {
             // Your log here
         }
    
         @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
         fun appInBackground() {
             // Your log here
         }
     }
    
  2. Add it to AndroidManifest:

     <applicattion
         android:name=".MyApplication" >
    
Fran Dioniz
  • 1,134
  • 9
  • 12
0

If the app is killed you will not be able to log anything because the OS process hosting the app is killed without warning.

You can determine when your app does to the background by using the solution described in this answer:

https://stackoverflow.com/a/42679191/769265

David Wasser
  • 93,459
  • 16
  • 209
  • 274