Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Android

एंड्रॉइड ऐप के SharedPreferences डेटा कैसे डिलीट करें

SharedPreferences से विशिष्ट कुंजियाँ या पूरा डेटा हटाने के तरीके

एंड्रॉइड में SharedPreferences डेटा हटाने के कई तरीके हैं: विशिष्ट कुंजी हटाना, सभी डेटा साफ करना, या डिफ़ॉल्ट वैल्यूज पर रीसेट करना। यह उदाहरण सभी विधियों को दर्शाता है।

चरण 1: नया प्रोजेक्ट बनाएं

Android Studio में प्रोजेक्ट बनाएं।

चरण 2: लेआउट फ़ाइल

res/layout/activity_main.xml - डेटा दिखाने और हटाने के बटनों के साथ:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp"
    android:gravity="center">

    <TextView
        android:id="@+id/tvData"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:gravity="center"
        android:textSize="16sp"
        android:textStyle="bold"
        android:text="SharedPreferences डेटा यहाँ दिखेगा" />

    <Button
        android:id="@+id/btnDeleteKey"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:text="विशिष्ट कुंजी हटाएं (देश)" />

    <Button
        android:id="@+id/btnClearAll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:text="सभी डेटा साफ करें (clear)" />

    <Button
        android:id="@+id/btnResetDefaults"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:text="डिफ़ॉल्ट वैल्यूज पर रीसेट" />

    <TextView
        android:id="@+id/tvAfterChange"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:gravity="center"
        android:textSize="16sp"
        android:textStyle="bold" />

</LinearLayout>

चरण 3: स्ट्रिंग्स संसाधन

res/values/strings.xml:

<resources>
    <string name="app_name">SharedPrefs Demo</string>
    <string name="pref_key_player">player_name</string>
    <string name="pref_key_country">country_name</string>
</resources>

चरण 4: MainActivity में पूरा लॉजिक

package com.app.sample;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private TextView tvData, tvAfterChange;
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;
    private Button btnDeleteKey, btnClearAll, btnResetDefaults;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tvData = findViewById(R.id.tvData);
        tvAfterChange = findViewById(R.id.tvAfterChange);
        btnDeleteKey = findViewById(R.id.btnDeleteKey);
        btnClearAll = findViewById(R.id.btnClearAll);
        btnResetDefaults = findViewById(R.id.btnResetDefaults);

        // डिफ़ॉल्ट SharedPreferences (एक्टिविटी के लिए प्राइवेट)
        sharedPreferences = getPreferences(Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();

        // प्रारंभिक डेटा डालें
        insertSampleData();
        displayData(tvData, "प्रारंभिक डेटा:");

        // बटन क्लिक लिस्टनर्स
        btnDeleteKey.setOnClickListener(v -> deleteSpecificKey());
        btnClearAll.setOnClickListener(v -> clearAllData());
        btnResetDefaults.setOnClickListener(v -> resetToDefaults());
    }

    private void insertSampleData() {
        editor.putString(getString(R.string.pref_key_player), "क्रिस्टियानो रोनाल्डो");
        editor.putString(getString(R.string.pref_key_country), "पुर्तगाल");
        editor.apply();
    }

    private void displayData(TextView target, String prefix) {
        String player = sharedPreferences.getString(getString(R.string.pref_key_player), "");
        String country = sharedPreferences.getString(getString(R.string.pref_key_country), "");

        String text = prefix + "\nखिलाड़ी: " + player + "\nदेश: " + country;
        target.setText(text);
    }

    // विधि 1: विशिष्ट कुंजी हटाएं
    private void deleteSpecificKey() {
        editor.remove(getString(R.string.pref_key_country));
        editor.apply();

        Toast.makeText(this, "देश कुंजी हटाई गई", Toast.LENGTH_SHORT).show();
        displayData(tvAfterChange, "देश हटाने के बाद:");
    }

    // विधि 2: सभी डेटा साफ करें (clear)
    private void clearAllData() {
        editor.clear();
        editor.apply();

        Toast.makeText(this, "सभी SharedPreferences डेटा साफ किया गया", Toast.LENGTH_SHORT).show();
        displayData(tvAfterChange, "Clear() के बाद:");
    }

    // विधि 3: डिफ़ॉल्ट वैल्यूज पर रीसेट (नई वैल्यूज डालकर)
    private void resetToDefaults() {
        editor.putString(getString(R.string.pref_key_player), "लियोनेल मेस्सी");
        editor.putString(getString(R.string.pref_key_country), "अर्जेंटीना");
        editor.apply();

        Toast.makeText(this, "डिफ़ॉल्ट वैल्यूज पर रीसेट किया गया", Toast.LENGTH_SHORT).show();
        displayData(tvAfterChange, "रीसेट के बाद:");
    }
}

मुख्य विधियों की तुलना

विधिउपयोगवापसी मान
editor.remove("key")एक विशिष्ट कुंजी-मान जोड़ी हटाता हैEditor (चेनिंग के लिए)
editor.clear()इस SharedPreferences की सभी कुंजियाँ हटाता हैEditor
editor.putString(key, default)नई डिफ़ॉल्ट वैल्यू सेट करता हैEditor

कॉमन शेयर्ड प्रीफरेंसेज (ग्लोबल) के लिए

// नामित SharedPreferences (ऐप-वाइड)
String PREFS_NAME = "my_app_prefs";
SharedPreferences prefs = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();

// फिर वही remove()/clear() विधियाँ काम करेंगी
editor.remove("specific_key").apply();
editor.clear().apply();

Kotlin एक्सटेंशन फंक्शन्स

// Kotlin में संक्षिप्त सिंटैक्स
val prefs = getSharedPreferences("my_prefs", Context.MODE_PRIVATE)

// विशिष्ट कुंजी हटाएं
prefs.edit { remove("key_name") }

// सभी साफ करें
prefs.edit { clear() }

// apply() स्वचालित रूप से कॉल होता है

चरण 5: AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.app.sample">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.AppCompat.Light">

        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

बेस्ट प्रैक्टिसेज

  • apply() असिंक्रोनस है (UI थ्रेड ब्लॉक नहीं करता), commit() सिंक्रोनस है
  • बड़े डेटा के लिए clear() की बजाय केवल आवश्यक कुंजियाँ हटाएं
  • लॉगआउट पर clear() का उपयोग करें, सेटिंग्स रीसेट के लिए remove()
  • डेटा माइग्रेशन के समय पुरानी कुंजियाँ remove() से हटाएं
नोट: getPreferences() एक्टिविटी-प्राइवेट SharedPreferences लौटाता है (नाम = एक्टिविटी क्लास नाम)। ऐप-वाइड शेयर्ड डेटा के लिए getSharedPreferences("name", MODE_PRIVATE) उपयोग करें।
  1. Android ऐप में JSONArray कैसे पार्स करें? Android ऐप में JSONArray कैसे पार्स करें?

    Android में JSON डेटा पार्स करना - JSONArray और JSONObject यह उदाहरण दर्शाता है कि Android में org.json पैकेज का उपयोग करके JSON स्ट्रिंग से डेटा कैसे निकाला जाए। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project चुनें। चरण 2: लेआउट फ़ाइल (res/layout/activity_main.xml) <?xml vers

  1. एंड्रॉइड में टोस्ट कैसे प्रदर्शित करें? एंड्रॉइड में टोस्ट कैसे प्रदर्शित करें?

    एंड्रॉइड में Toast संदेश दिखाने का बुनियादी उदाहरण यह उदाहरण दर्शाता है कि Toast.makeText() का उपयोग करके सरल टोस्ट संदेश कैसे प्रदर्शित करें। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project चुनें। चरण 2: लेआउट (ConstraintLayout) res/layout/activity_main.xml: <?xml version=1.0

  1. Android में 5 सेकंड यूजर इनएक्टिविटी कैसे डिटेक्ट करें? Android में 5 सेकंड यूजर इनएक्टिविटी कैसे डिटेक्ट करें?

    यह उदाहरण दर्शाता है कि Handler और onUserInteraction() का उपयोग करके Android में यूजर इनएक्टिविटी (कोई टच/की इवेंट नहीं) को कैसे डिटेक्ट किया जाता है। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project। चरण 2: लेआउट XML res/layout/activity_main.xml: <?xml version=1.0 encoding=utf-