Martijn
Martijn

Reputation: 155

Application lifecycle, setting a timer to logout the user

I'm pretty new to android and currently I ran into a problem concerning login and logout of a user. I have these activities:

The idea is that when a user goes to transactions a login is requested. After the user is logged in he stays login while he uses the app. Even if he stays on a page and does nothing for multiple minutes. When the app locks or closes the user is logged out after 60 seconds. When he comes back in time he stays logged in, otherwise a new login form is shown.

So I was thinking on how to solve this. My first thoughts were to set a timer when the app locks/is closed. When the user comes back the timer is checked. If after 60 seconds the user details (in memory) are thrown away.

I think it has something to do with the application lifecycle, maybe a static variable. But I don't know where to start.

Has anyone got a solution or a tip so I can finish this thing?? Would be greatly appreciated

Upvotes: 2

Views: 163

Answers (3)

Anmol Bhardwaj
Anmol Bhardwaj

Reputation: 674

Service Class is the answer to your problem.

Create a background service which starts each time you log in and write the timer code there.

Reference for creating service:

https://developer.android.com/training/run-background-service/create-service.html

Upvotes: 0

Sunil Soni
Sunil Soni

Reputation: 443

You can save the timestamp while user login and also save the max session time in shared preference. When the user tries to do transaction again you can check the timestamp with current time and session. if the time difference is greater than session time just ask for login.

Upvotes: 0

Arslan Ali
Arslan Ali

Reputation: 371

Use Shared Preference in this case Note the current time when the app closes and on Restarting the app compare the current time with the stoppage time and then find the difference in time. If it is greater than your demand then Request Login Page. If you want sample code for difference of time I can edit my Post

Upvotes: 1

Related Questions