從android中的共享首選項中刪除數據

[英]Delete data from shared preferences in android


I am trying to delete the data from shared preferences. But I cant do that. To track that the data is removed or not, I am using this code:

我試圖從共享首選項中刪除數據。但我不能這樣做。要跟蹤數據是否被刪除,我使用此代碼:

btnLogout.setOnClickListener(new View.OnClickListener() {

    public void onClick(View arg0) {
            SharedPreferences prefs = getSharedPreferences(share_pref_file, Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = prefs.edit();
            editor.remove(share_pref_file);
            editor.clear();
            editor.commit();
            getApplicationContext().getSharedPreferences(share_pref_file, 0).edit().clear().commit(); 
                String strJson = prefs.getString("jsondata","");
                if(strJson != null) 
                    {
                     Log.d("CLEAR", "cccccccccccccccccccccccccccc");
                    }
                userFunctions.logoutUser(getApplicationContext());
                Intent login = new Intent(getApplicationContext(),LoginActivity.class);
                login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(login);
                // Closing dashboard screen
                finish();
        }
});

But in logcat window it is showing me "cccccccccccccccccccccccccccc" value every time.

但是在logcat窗口中,每次都顯示“cccccccccccccccccccccccccccc”值。

So can anyone help me out that how to remove/delete the data from shared preferences so that if I click on 'logout' button it will remove all the stored data? Thanks in advance.

所以任何人都可以幫我解決如何從共享首選項中刪除/刪除數據,這樣如果我點擊“注銷”按鈕,它將刪除所有存儲的數據?提前致謝。

6 个解决方案

#1


2  

An empty string is still a string (and String("") != null will return true). Try this instead:

空字符串仍然是一個字符串(而String(“”)!= null將返回true)。試試這個:

if(!strJson.equals("")) 

This assumes the empty string will never be a valid input in your SharedPreferences in the first place.

這假設空字符串首先不會是您的SharedPreferences中的有效輸入。

#2


1  

Try this one inside the button click event to clear the SharedPreferences values.

在按鈕單擊事件中嘗試此操作以清除SharedPreferences值。

Editor editor = getSharedPreferences("MyPref", Context.MODE_PRIVATE).edit();
            editor.clear();
            editor.commit();

#3


1  

Try editor.clear(); followed by a editor.commit();

試試editor.clear();然后是editor.commit();

Here is one example that I've used:

這是我用過的一個例子:

Preference clearPref = (Preference) findPreference("MyPref");

   btnLogout.setOnPreferenceClickListener(new OnPreferenceClickListener() {

        public boolean onPreferenceClick(Preference preference) {
            SharedPreferences settings =  PreferenceManager.getDefaultSharedPreferences(getBaseContext());                          
            SharedPreferences.Editor editor = settings.edit();
            editor.clear();
            editor.commit();                
            Toast.makeText(getBaseContext(), "All data cleared!", Toast.LENGTH_SHORT).show();

            return true;
        }

    });

#4


0  

Either change this:

要么改變這個:

String strJson = prefs.getString("jsondata", "");

To:

至:

String strJson = prefs.getString("jsondata", null);

And then check:

然后檢查:

if(strJson != null) {
    Log.d("CLEAR", "cccccccccccccccccccccccccccc");
}

Or keep it as it is and check it like this:

或保持原樣,並檢查如下:

if(strJson.equals("")) {
    Log.d("CLEAR", "cccccccccccccccccccccccccccc");
}

#5


0  

settings.edit().clear().commit();

is a one line code I am using, works perfectly

是我正在使用的一行代碼,完美運行

#6


0  

It is my solution:

這是我的解決方案:

You can put null instead of your favorite data in shareprefrences

您可以在shareprefrences中將null而不是您喜歡的數據放入

getApplicationContext();
SharedPreferences.Editor pref = (Editor) getSharedPreferences("data", MODE_PRIVATE).edit();
pref.putString("admin_no", null);
pref.commit();

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2013/04/02/8ac1fb1dbc8c324ce20fba13fd2f4b04.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com