Android 注销清除 SharedPreferences

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24163708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:51:47  来源:igfitidea点击:

Logout clear SharedPreferences

androidsharedpreferenceslogout

提问by HurkanSeyhan

I have a login page that saves usernameand passwordto SharedPreferences. I have another Activityclass that includes a logout button. I want to clear SharedPreferenceswhen I click the logout button. The problem is that I don't get the SharedPreferencesfrom this class. How can I get the SharedPreferences?

我有一个登录页面,可以保存usernamepasswordSharedPreferences. 我还有一个Activity包含注销按钮的类。我想SharedPreferences在单击注销按钮时清除。问题是我没有SharedPreferences从这个类中得到。我怎样才能得到SharedPreferences

LoginPage

登录页面

public class MainActivity extends Activity {
public SharedPreferences.Editor loginPrefsEditor;
    public  SharedPreferences loginPreferences;
    private Boolean saveLogin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    name = (EditText) findViewById(R.id.et_Username);
        pass = (EditText) findViewById(R.id.et_Password);
        login = (Button) findViewById(R.id.bt_Login);

         loginPreferences = getSharedPreferences("loginPrefs", MODE_PRIVATE);
         loginPrefsEditor = loginPreferences.edit();

         saveLogin = loginPreferences.getBoolean("saveLogin", false);

         if (saveLogin == true) {
                name.setText(loginPreferences.getString("username", ""));
                pass.setText(loginPreferences.getString("password", ""));   
            }

        login.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v) {
                 name1 = name.getText().toString();
                 pass1 = pass.getText().toString();
               //new Thread (new Task()).start();
                    loginPrefsEditor.putBoolean("saveLogin", true);
                    loginPrefsEditor.putString("username", name1);
                    loginPrefsEditor.putString("password", pass1);
                    loginPrefsEditor.commit();
                 new myAsyncTask().execute();
            }
        });
}

Logout Button in AnotherActivity

另一个活动中的注销按钮

 logout.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                // Launching News Feed Screen

                Intent i = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(i);
            }
        });

回答by Krupa Patel

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();
 });

回答by Shubhang Malviya

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

我认为您在理解 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.

要为您的应用程序获取 SharedPreferences 对象,请使用以下两种方法之一:

getSharedPreferences() - 如果您需要通过名称标识的多个首选项文件,请使用此选项,您可以使用第一个参数指定该名称。

getPreferences() - 如果您的 Activity 只需要一个首选项文件,请使用此选项。因为这将是您的活动的唯一首选项文件,所以您无需提供名称。

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#prefand http://www.vogella.com/tutorials/AndroidFileBasedPersistence/article.html

有关更多详细信息和更好的解释,您可以在此处阅读http://developer.android.com/guide/topics/data/data-storage.html#prefhttp://www.vogella.com/tutorials/AndroidFileBasedPersistence/article .html

Hope this will help.

希望这会有所帮助。

Cheers!

干杯!

回答by Piyush

It as Simple. Like you save your data in SharedPrefernce

它很简单。就像您将数据保存在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();

回答by Vipin Yadav

SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = mPrefs.edit();
editor.clear();
editor.commit();

回答by prijupaul

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

为什么不编写 SharedPreference 实用程序类。这可以从两个活动中访问。