Saving user information in app settings

后端 未结 4 634
一个人的身影
一个人的身影 2020-12-04 14:26

i am creating login in my app. User type his info i send it to web service to check and get his ID. Now i need to save it somewhere in my app that i could check if he had lo

相关标签:
4条回答
  • 2020-12-04 15:04

    You can use SharedPreferences for this.

    Some codes from references (taken from documentation):

    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();
        }
    }
    
    0 讨论(0)
  • 2020-12-04 15:09

    if user clear the application data from setting -> application manager -> your application -> clear data

    then all data saved in shared preferences will get removed

    0 讨论(0)
  • 2020-12-04 15:19

    Answer is Use Application Class or Shared Preferences

    I assume you can easily search for the Example of Application Class, SharedPreferences and SQLite Database

    So I am mentioning Where Do I Use Which Options

    Application Class

    When I got anything(var , array , list , objects) to store just only for the LifeTime of the Application..and As it got close (for Android Force Close) I use Application Class.

    SharedPreferences

    I use this when I got some short information like id,token,username,flags and I want to store that as long as the application rest in the Device..and need to use those information everytime I start the Application..

    SQLite Database

    When there is a bulk data in Record format (Row - Column) I used it to store in database so it becomes easy to store , retrive and manipulate.

    0 讨论(0)
  • 2020-12-04 15:28

    First of all save user info like uname & password in SharedPreferences with this code.

    SharedPreferences settings = getSharedPreferences("UserInfo", 0);
    SharedPreferences.Editor editor = settings.edit();
    editor.putString("Username",txtUname.getText().toString());
    editor.putString("Password",txtPWD.getText().toString());
    editor.commit();
    

    and then get SharedPreferences from below code

    SharedPreferences settings = getSharedPreferences("UserInfo", 0);
    txtUname.setText(settings.getString("Username", "").toString());
    txtPWD.setText(settings.getString("Password", "").toString());
    
    0 讨论(0)
提交回复
热议问题