0

I want to store user logged out time in the database(for maintaining the user analytics) when the android app forcibly closed by user. Is it possible to capture event in MainActivity?

can any one suggest the better approach to achieve this?

vishnu
  • 4,377
  • 15
  • 52
  • 89
  • Please check this post. It explain briefly https://stackoverflow.com/questions/36093957/logout-user-when-app-gets-killed – MRazaImtiaz Nov 25 '20 at 05:42

1 Answers1

0

there is no reliable way for this event detection - this would be kind-of insecure to inform app that it's going to be killed allowing to execute some code at that moment (as in your case - database handling). user clearly wants to stop this app, when it would get a possibility to execute some code just before gets killed then it may e.g. leave some alarm for now + 10secs in AlarmManager to restart some background service. this is opposite to what user wanted

best you can do is listening when last Activity is destroyed (onDestroy call), but this may occur in multiple cases, no ony force stop app by user. use some static int and ++ when Activity is created and -- when destroyed or use Application.ActivityLifecycleCallbacks on never APIs

snachmsm
  • 17,866
  • 3
  • 32
  • 74