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?
Asked
Active
Viewed 709 times
0
-
Use `ProcessLifecycleOwner`. – CommonsWare Feb 10 '21 at 23:07
2 Answers
0
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 } }
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:

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