Logout clear SharedPreferences

前端 未结 6 2090
清歌不尽
清歌不尽 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 02:54

    Try this !

    logout.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View view) {
                // Launching News Feed Screen
    
                 SharedPreferences preferences =getSharedPreferences("loginPrefs",Context.MODE_PRIVATE);
                 SharedPreferences.Editor editor = preferences.edit();
                 editor.clear();
                 editor.apply();
                 finish();
     });
    
    0 讨论(0)
  • 2021-02-07 02:54

    I think you have a trouble in understanding Shared preferences in android .

    According to official documentation

    To get a SharedPreferences object for your application, use one of two methods:

    getSharedPreferences() - Use this if you need multiple preferences files identified by name, which you specify with the first parameter.

    getPreferences() - Use this if you need only one preferences file for your Activity. Because this will be the only preferences file for your Activity, you don't supply a name.

    You should have a Context for using both the above methods .

    Also Shared preferences are stored asa key value pair , so clearing should mean that you set the values to some empty string.

    For more details , and better explanation you can read here http://developer.android.com/guide/topics/data/data-storage.html#pref and http://www.vogella.com/tutorials/AndroidFileBasedPersistence/article.html

    Hope this will help.

    Cheers!

    0 讨论(0)
  • 2021-02-07 03:02

    In Kotlin you can use this code to clear the sharedpreference data

    private val sharedPref = "sharedpreference"
    
    val sharedPreferences: SharedPreferences = context.getSharedPreferences(sharedPref, Context.MODE_PRIVATE)
                val editor = sharedPreferences.edit()
                editor.clear()
                editor.apply()
    
    0 讨论(0)
  • 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();
    
    0 讨论(0)
  • 2021-02-07 03:04
    SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = mPrefs.edit();
    editor.clear();
    editor.commit();
    
    0 讨论(0)
  • 2021-02-07 03:07

    Why not write a SharedPreference utility class. This can be accessed from both the activities.

    0 讨论(0)
提交回复
热议问题