What would be the best way to time how long a user spends in a specific Activity?
Thanks in advance!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I guess I’d start out with something like this:
Note that because it’s using onPause/onResume it will also ‘pause’ the timer if the screen turns off, if it re-orients etc. Depending on what your purpose is you may want to use one of the other callbacks like onCreate() or onDestroy(), but note that onDestroy() doesn’t alway get called when a new activity launches from the existing activity.
You may also want to save mTotalTime to SharedPreferences so that it is preserved even if the app is shut down. Maybe something like this: