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

एंड्रॉइड में एक्टिविटीज के बीच SharedPreferences का उपयोग कैसे करें?

SharedPreferences से डेटा शेयर करना

SharedPreferences छोटे प्रिमिटिव डेटा (String, int, boolean, float, long) को की-वैल्यू पेयर के रूप में स्टोर करने का हल्का तरीका है। यह एक्टिविटीज, यहां तक कि ऐप रीस्टार्ट के बाद भी डेटा एक्सेस करने देता है।

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

चरण 2: लेआउट (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_margin="16dp"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/edit_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:hint="पास करने के लिए कुछ दर्ज करें"
        android:inputType="text" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="16dp"
        android:text="अगला" />
</LinearLayout>

चरण 3: MainActivity.java (डेटा सेव करना)

package com.example.myapplication;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        EditText editText = findViewById(R.id.edit_text);
        Button button = findViewById(R.id.button);

        button.setOnClickListener(v -> {
            String value = editText.getText().toString().trim();
            SharedPreferences sharedPref = getSharedPreferences("myKey", MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPref.edit();
            editor.putString("value", value);
            editor.apply(); // असिंक्रोनस सेव

            Intent intent = new Intent(MainActivity.this, SecondActivity.class);
            startActivity(intent);
        });
    }
}

चरण 4: सेकंड एक्टिविटी लेआउट (res/layout/activity_second.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_margin="16dp"
    android:orientation="vertical"
    tools:context=".SecondActivity">

    <TextView
        android:id="@+id/text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center" />
</LinearLayout>

चरण 5: SecondActivity.java (डेटा पढ़ना)

package com.example.myapplication;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

public class SecondActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        TextView textView = findViewById(R.id.text_view);
        SharedPreferences sharedPreferences = getSharedPreferences("myKey", MODE_PRIVATE);
        String value = sharedPreferences.getString("value", ""); // डिफ़ॉल्ट खाली स्ट्रिंग
        textView.setText(value);
    }
}

चरण 6: AndroidManifest.xml

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

    <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>
        <activity android:name=".SecondActivity"></activity>
    </application>
</manifest>

मुख्य बिंदु

  • MODE_PRIVATE - केवल आपका ऐप एक्सेस कर सकता है (सुरक्षित)।
  • apply() vs commit() - apply() असिंक्रोनस है (UI थ्रेड ब्लॉक नहीं करता), commit() सिंक्रोनस है।
  • कॉम्प्लेक्स ऑब्जेक्ट्स के लिए Gson से JSON में कन्वर्ट करके स्टोर करें।
  • बड़े डेटा के लिए Room Database या DataStore (Jetpack) बेहतर विकल्प हैं।
  1. Android में ड्रॉएबल (Drawable) को एक्टिविटी के बीच कैसे पास करें? Android में ड्रॉएबल (Drawable) को एक्टिविटी के बीच कैसे पास करें?

    Android में Intent के माध्यम से ड्रॉएबल संसाधन पास करने का गाइड यह ट्यूटोरियल दिखाता है कि कैसे ड्रॉएबल रिसोर्स ID (इंटीजर) को Intent extra के रूप में दूसरी एक्टिविटी में पास किया जाए और वहां ImageView में सेट किया जाए। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project चुनकर नया प्रो

  1. Android में एक्टिविटी के बीच स्विच कैसे करें? Android में एक्टिविटी के बीच स्विच कैसे करें?

    एक्टिविटी के बीच नेविगेशन यह उदाहरण दर्शाता है कि Intent का उपयोग करके दो एक्टिविटी के बीच आगे-पीछे कैसे नेविगेट करें। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project चुनें। चरण 2: मुख्य लेआउट (activity_main.xml) <?xml version=1.0 encoding=utf-8?> <RelativeLayout xmlns:an

  1. Android में NavigationView (नेविगेशन ड्रॉअर) कैसे उपयोग करें? Android में NavigationView (नेविगेशन ड्रॉअर) कैसे उपयोग करें?

    परिचय NavigationView मटेरियल डिज़ाइन का साइड नेविगेशन ड्रॉअर (हैमबर्गर मेनू) है। यह DrawerLayout के अंदर होता है और मेनू आइटम्स, हेडर, और नेविगेशन हैंडलिंग प्रदान करता है। यह ट्यूटोरियल Navigation Components के साथ आधुनिक सेटअप दिखाता है। आवश्यक चरण चरण 1: नया प्रोजेक्ट बनाएं Android Studio में Fi