Android Shared Preferences में apply() मेथड का उपयोग
Shared Preferences apply() में जाने से पहले, हमें पता होना चाहिए कि Android में Shared Preferences क्या है। Shared Preferences का उपयोग करके, हम मानों को कुंजी और मान जोड़ी के रूप में संग्रहीत या पुनर्प्राप्त कर सकते हैं। Shared Preferences में पांच अलग-अलग मेथड्स उपलब्ध हैं जैसा कि नीचे दिखाया गया है -
- edit() - यह Shared Preferences मानों को संपादित करने के लिए उपयोग किया जाता है
- commit() - यह Shared Preferences मानों को XML फ़ाइल में कमिट करता है
- apply() - यह संपादक से Shared Preferences में परिवर्तनों को कमिट करता है
- remove(String key) - यह कुंजी का उपयोग करके Shared Preferences से कुंजी और मानों को हटाता है
- put() - यह कुंजी और मानों को Shared Preferences XML में डालता है
Shared Preferences का एक नमूना उदाहरण सिंटैक्स नीचे दिखाया गया है -
final SharedPreferences sharedPreferences = getSharedPreferences("USER", MODE_PRIVATE);
उपरोक्त सिंटैक्स में हमने USER.xml के रूप में एक Shared Preferences फ़ाइल बनाई है और यह प्राइवेट मोड है मतलब कोई अन्य एप्लिकेशन इस Shared Preferences को एक्सेस नहीं कर सकता।
Shared Preferences में apply() मेथड का उपयोग -
apply() इन-मेमोरी स्टोरेज में तुरंत लिखता है, और स्थायी स्टोरेज में एक अतुल्यकालिक (asynchronous) लिखने को शेड्यूल करता है।
यह नीचे उदाहरण दर्शाता है कि Android Shared Preferences में apply() का उपयोग कैसे करें।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में एक नया प्रोजेक्ट बनाएं: File → New Project पर जाएं और नया प्रोजेक्ट बनाने के लिए सभी आवश्यक विवरण भरें।
चरण 2: लेआउट XML जोड़ें (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"
tools:layout_editor_absoluteY="81dp">
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="8dp"
android:autofillHints=""
android:hint="NAME"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="0dp" />
<EditText
android:id="@+id/address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="84dp"
android:hint="Phone Number"
android:importantForAutofill="no"
android:inputType=""
app:layout_constraintTop_toTopOf="@id/name"
tools:layout_editor_absoluteX="16dp"
tools:targetApi="o" />
<Button
android:id="@+id/button"
android:layout_width="108dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="120dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:gravity="center_horizontal"
android:text="Save"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@id/address" />
<Button
android:id="@+id/read"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="88dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:gravity="center_horizontal"
android:text="read"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/button" />
<TextView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="184dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="result"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/button" />
</android.support.constraint.ConstraintLayout>
उपरोक्त XML में नाम और पते के लिए दो EditText हैं। जब उपयोगकर्ता सेव बटन पर क्लिक करता है तो यह मानों को Shared Preferences में संग्रहीत करेगा और जब उपयोगकर्ता रीड बटन पर क्लिक करता है तो यह Shared Preferences से मानों को पढ़ेगा।
चरण 3: MainActivity.java कोड जोड़ें (src/MainActivity.java)
package com.example.andy.myapplication;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final SharedPreferences sharedPreferences = getSharedPreferences("USER", MODE_PRIVATE);
final EditText name = findViewById(R.id.name);
final EditText address = findViewById(R.id.address);
final TextView result = findViewById(R.id.result);
Button save = findViewById(R.id.button);
Button read = findViewById(R.id.read);
read.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
result.setText("Name is " + sharedPreferences.getString("Name", "No name") + " Address " +
sharedPreferences.getString("Address", "No Address"));
}
});
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (name.getText().toString().isEmpty() && address.getText().toString().isEmpty()) {
Toast.makeText(MainActivity.this, "Plz Enter all the data", Toast.LENGTH_LONG).show();
} else {
String nameData = name.getText().toString().trim();
String addressData = address.getText().toString().trim();
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("Name", nameData);
editor.putString("Address", addressData);
editor.apply();
}
}
});
}
}
चरण 4: Manifest.xml में कोई बदलाव की आवश्यकता नहीं
आइए अपने एप्लिकेशन को चलाने का प्रयास करें। मान लें कि आपने अपने वास्तविक Android मोबाइल डिवाइस को अपने कंप्यूटर से कनेक्ट किया है। Android Studio से ऐप चलाने के लिए, अपनी प्रोजेक्ट की एक्टिविटी फ़ाइलों में से एक खोलें और टूलबार से Run आइकन पर क्लिक करें। अपने मोबाइल डिवाइस को एक विकल्प के रूप में चुनें और फिर अपने मोबाइल डिवाइस की जांच करें जो आपकी डिफ़ॉल्ट स्क्रीन प्रदर्शित करेगा -

उपरोक्त उदाहरण में हमने नाम और पता जोड़ा और सेव बटन पर क्लिक किया।

उपरोक्त उदाहरण में, हमने रीड बटन पर क्लिक किया। यह टेक्स्ट को TextView में जोड़ देगा।