Skip to content Skip to sidebar Skip to footer

Check If User Is Logged In On Every Activity, Or Only At The Beginning?

What is the correct way of checking if a user is logged in: To check once in the main screen if he's logged in or not, and if he is logged in, send him to the app's logged-in flow,

Solution 1:

Should start checking at the beginning for this purpose. This process will execute in SplashActivity class. If the user already checked in it will for next page or login page.

Solution 2:

As soon as the user is logged in you need to store log in credentials stuff in persistent storage like Shared Preference. As soon as the user starts the app (normally on splash screen or your main activity) get value from shared preference. If the value is available that means user is logger in. But when user gets log out via your app. You need to clear shared preference value and send the user to log in screen. After log out if user try use your app, there will be no value is shared preference, then rather navigating user to the app, navigate user to log in activity. As far as the matter of checking user log in in every activity is concerned. it is no mandatory in your case. If there is a business need (if admin do not allow from server backened then user is not able to use the app) then you need to check in onCreate() method. There should be a base class and your every child class should extend you base classs by this you will not need to check on every activity. In ** base class on create method** you need hit service that will verify if the user is allow to use your app further. If the server response (normally a boolean value) is false then you can navigate to user to the log in activity and also clear shared preference stuff. Hope this helps you.

Solution 3:

I used to have a BaseActivity that was parent of all the other activites and I checked app health and the log-in token in its onCreate method.

Also, if you use Firebase, you can create an Application Class and set the onAuthStateChanged listener in onCreate() and act accordingly in that method.

Post a Comment for "Check If User Is Logged In On Every Activity, Or Only At The Beginning?"