Keep the account info in all activities in android studio

前端 未结 2 1586
庸人自扰
庸人自扰 2021-01-24 09:20

My application is simply a managing database application. I have a remote mysql server set up and my android studio application uses http post request to connect to that server.

相关标签:
2条回答
  • 2021-01-24 09:42

    Many options:

    1) Create a global class with static variables.

    2) Extend the Application class and store information in it.

    2) If it's just two-three parameters, you can store it in shared preferences.

    0 讨论(0)
  • 2021-01-24 09:43

    you could it done in many ways like store data in sharedpreference or database or simply create a class that contains static variables.

    To store and get data from shared preference use this.

    public class MyPreference {
    
    SharedPreferences myPrefs;
    SharedPreferences.Editor prefEditor;
    Context context;
    
    // ------------------------------------------------------------------costructor
    public MyPreference(Context context) {
        this.context = context;
        myPrefs = context.getSharedPreferences("myPrefs", 0);
    }
    
    // ----------------------------------------------------------------------- user login
    
    public void setLogin() {
    
        prefEditor = myPrefs.edit();
        prefEditor.putBoolean("login", true);
        prefEditor.commit();
    
    }
    
    public boolean isLogin() {
        return myPrefs.getBoolean("login", false);
    }
    
    public void setPassword(String userPass) {
        prefEditor = myPrefs.edit();
        prefEditor.putString("user", userPass);
        prefEditor.commit();
    }
    
    public String getPassword() {
        return myPrefs.getString("user", "");
    }
    }
    
    0 讨论(0)
提交回复
热议问题