Logout clear SharedPreferences

前端 未结 6 2163
清歌不尽
清歌不尽 2021-02-07 02:43

I have a login page that saves username and password to SharedPreferences. I have another Activity class that includes a logo

6条回答
  •  無奈伤痛
    2021-02-07 03:03

    It as Simple. Like you save your data in SharedPrefernce

    SharedPreferences sp = getSharedPreferences("MYKEY",0);
    SharedPreferences.Editor editor = sp.edit();
    editor.putString("username" , username);
    editor.putString("password" , password);
    

    Now you can retrieve as in any class of your app like,

    SharedPreferences sp = getSharedPreferences("MYKEY",0);
    String uname = sp.getString("username");
    String pwd = sp.getString("password");
    

    And for clear your username and password

    editor.clear();
    editor.commit();
    

    or

    editor.remove("username");
    editor.remove("password");
    editor.commit();
    

提交回复
热议问题