Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Best Way to Detect and Handle User INACTIVITY

Tags:

android

Inactivity is a very important EVENT. For many apps if the user does not interact with it for a certain number of seconds its time to reset the app and go back to main activity logout, or conserve power. So I would really like to get some feedback on the best way to detect this. In fact I think everyone will benefit from a good solution to this.

So my question is twofold:

1) Is there a better way to detect user inactivity than using a combination of activity.onUserInteraction() to reset a CountDownTimer?

Note: One reported downside to this approach is that softkeypad interaction might not be caught by this approach.

Note: Another reported downside is the CountDownTimer is off main thread and might not update correctly. I am not sure how big an issue this is?

Note: CountDownTimer appears to have cancellation issues as well: how to stop/cancel android CountDownTimer

2) Lets say that onUserInteraction()/CountDownTimer is the best/only solution to this problem there are still some questions:

a) should each activity launch its own countdown timer?

b) Should a single countdown timer be restarted in the onCreate method of each activity?

c) lets say I want to dim the screen or goto main activity when the countdown expires where should the timeout handler be located? In each activity? In a service?

Thanks

like image 891
Androider Avatar asked Apr 16 '11 21:04

Androider


People also ask

How do you detect user inactivity in flutter?

And a good way to do this is to use the mixin WidgetsBindingObserver on your LandingPage of the app. The mixin provides you with an enum AppLifecycleState which can have 4 values, detached, inactive, paused and resumed which depicts the current state of the app.


1 Answers

Just stumbled upon this question as I've answered something similar just now.

Personally, I'd opt for option 2 that you have suggested, and put a timer into a singleton so its available across all activities. Theres no need for a separate countdown timer unless you have a specific requirement to react different under different features of your application.

Why would you want to reset the timer in the onCreate? You should do that each time the user interacts with the application, such as in the activity.onUserInteraction() method.

To quote from my previous answer:

You'll need to invest a little thought into exactly what your requirements are here, but from what I can tell, you want to keep track of the user interactions and if a time limit expires since the last interaction, perform some action, in your case logging them out of your application.

Firstly, you'll need some place that you can track when the last interaction occured, since you'll want this to be application wide you could use a singleton to hold this, or override the Application class, either way should do.

Next, you'll need to start tracking user interactions. From your activities, you can override the onUserInteraction method, this gets invoked anytime the user interacts with the application such as key event. Each time you hit this method, update your singleton and let it know something has happened, with a timestamp.

Finally, you'll need some kind of looping check to constantly check if anything has happened recently. Theres various was of doing this, you could have a continuous loop that compares current timestamp to the last recorded event, a bit of draft code :

while(true)
{
   if (timeLastEventRecorded < (now - 15))
   {
      //nothing has happened in 15 minutes, so take corrective action
   }
}

Presumably you'll already have some code in your application that takes care of logouts, such as when the user clicks "logout", you should just be able to invoke that in the sample above.

like image 172
Jimmy Avatar answered Oct 19 '22 02:10

Jimmy