How to save a user login session?

I have an application that requires user registration. I have an application related to PHP on my server to execute registration / login logic and so, this is not a problem at all.

But I want the user session to be local, so the user does not need to log in again every time the application starts.

So I want something like this:

  • The first user, he opens the application, registers and logs into the system.
  • Do some things with the application and close it.
  • Open the application again, the user is recognized, so he does not need to log in again.

I only need to save the identifier and username (both extracted from the database in the php login method). What is the best way to do this?

I was thinking about making user preferences, storing data in files, or even a local database (sqlite). Any suggestions?

+3
source share
2 answers

Yes, preferences will work. In onCreate():

mPrefs = PreferenceManager.getDefaultSharedPrefs(this);

And in some function that is called after the user logs in:

SharedPreferences.Editor edit = mPrefs.edit();
edit.putString("app_username", username);
edit.putInt("app_userid", userId);
edit.commit();
+6
source

Use general preferences for this purpose. try under the code ....:

PreferenceManager pm = PreferenceManager.getDefaultSharedPrefs(this);
SharedPreferences.Editor edit = pm.edit();
edit.putString("user", username);
edit.putInt("pwd", password);
edit.commit();
0
source

Source: https://habr.com/ru/post/1740469/


All Articles