Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store login credentials android

Tags:

android

I have made an android app which working fine. I implemented the Login functionality in it. Can anyone guide me how to store the login credentials of the user so that the user does not have to login everytime he/she starts the app.

like image 857
Rahul Sharma Avatar asked Feb 23 '23 13:02

Rahul Sharma


1 Answers

Here's an example from the Android developer site:

public class Calc extends Activity {
    public static final String PREFS_NAME = "MyPrefsFile";

    @Override
    protected void onCreate(Bundle state){
       super.onCreate(state);
       . . .

       // Restore preferences
       SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
       boolean silent = settings.getBoolean("silentMode", false);
       setSilent(silent);
    }

    @Override
    protected void onStop(){
       super.onStop();

      // We need an Editor object to make preference changes.
      // All objects are from android.context.Context
      SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
      SharedPreferences.Editor editor = settings.edit();
      editor.putBoolean("silentMode", mSilentMode);

      // Commit the edits!
      editor.commit();
    }
}

I recommend the Shared Preferences, but also take a look at the other options on that page to see if they fit your needs better.

like image 96
Klox Avatar answered Mar 12 '23 07:03

Klox