परिचय
SharedPreferences Android में छोटी मात्रा में प्राइमिटिव डेटा (बूलियन, फ्लोट, इंट, लॉन्ग, स्ट्रिंग) को की-वैल्यू पेयर के रूप में स्टोर करने का एक सरल तरीका है। यह डेटा ऐप रीस्टार्ट के बाद भी बना रहता है। इस ट्यूटोरियल में हम नाम और ईमेल सेव करना, पढ़ना और क्लियर करना सीखेंगे।
आवश्यक चरण
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project से नया प्रोजेक्ट बनाएं।
चरण 2: लेआउट फ़ाइल (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp"
tools:context=".MainActivity">
<EditText
android:id="@+id/etName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/etEmail"
android:layout_marginTop="40dp"
android:ems="10"
android:hint="नाम"
android:inputType="text" />
<EditText
android:id="@+id/etEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/etName"
android:layout_alignParentEnd="true"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="ईमेल"
android:inputType="textEmailAddress" />
<Button
android:id="@+id/btnSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerInParent="true"
android:onClick="Save"
android:text="सेव करें" />
<Button
android:id="@+id/btnRetrieve"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:onClick="Get"
android:text="पढ़ें" />
<Button
android:id="@+id/btnClear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerInParent="true"
android:onClick="clear"
android:text="क्लियर करें" />
</RelativeLayout>
चरण 3: मुख्य गतिविधि (src/MainActivity.java)
package app.com.sample;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText editTextName, editTextEmail;
public static final String mypreference = "mypref";
public static final String Name = "nameKey";
public static final String Email = "emailKey";
SharedPreferences sharedpreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextEmail = findViewById(R.id.etEmail);
editTextName = findViewById(R.id.etName);
// SharedPreferences इंस्टेंस प्राप्त करें (प्राइवेट मोड में)
sharedpreferences = getSharedPreferences(mypreference, Context.MODE_PRIVATE);
// ऐप शुरू होने पर सेव्ड डेटा लोड करें
if (sharedpreferences.contains(Name)) {
editTextName.setText(sharedpreferences.getString(Name, ""));
}
if (sharedpreferences.contains(Email)) {
editTextEmail.setText(sharedpreferences.getString(Email, ""));
}
}
// सेव बटन क्लिक
public void Save(View view) {
String strName = editTextName.getText().toString();
String strEmail = editTextEmail.getText().toString();
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Name, strName);
editor.putString(Email, strEmail);
editor.apply(); // एसिंक्रोनस सेव
Toast.makeText(getApplicationContext(), "सेव हो गया", Toast.LENGTH_SHORT).show();
}
// क्लियर बटन क्लिक
public void clear(View view) {
editTextName.setText("");
editTextEmail.setText("");
Toast.makeText(getApplicationContext(), "क्लियर हो गया", Toast.LENGTH_SHORT).show();
}
// पढ़ें बटन क्लिक
public void Get(View view) {
sharedpreferences = getSharedPreferences(mypreference, Context.MODE_PRIVATE);
if (sharedpreferences.contains(Name)) {
editTextName.setText(sharedpreferences.getString(Name, ""));
}
if (sharedpreferences.contains(Email)) {
editTextEmail.setText(sharedpreferences.getString(Email, ""));
}
Toast.makeText(getApplicationContext(), "डेटा प्राप्त हुआ", Toast.LENGTH_SHORT).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
}
चरण 4: मैनिफेस्ट फ़ाइल (AndroidManifest.xml)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.com.sample">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
ऐप चलाएं
डिवाइस कनेक्ट करें और Run पर क्लिक करें। नाम और ईमेल दर्ज करें, सेव करें दबाएं, ऐप बंद करके फिर खोलें - डेटा बना रहेगा।

महत्वपूर्ण बिंदु
Context.MODE_PRIVATE- केवल आपका ऐप इस डेटा को एक्सेस कर सकता है।editor.apply()- एसिंक्रोनस (नॉन-ब्लॉकिंग) सेव करता है।commit()सिंक्रोनस है।- SharedPreferences बड़े डेटा या कॉम्प्लेक्स ऑब्जेक्ट्स के लिए उपयुक्त नहीं है - उसके लिए Room डेटाबेस उपयोग करें।
- कुंजी नाम (
nameKey,emailKey) यूनिक होने चाहिए।