SharedPreferences দিয়ে ব্যবহারকারীর প্রমাণপত্র সংরক্ষণ করা
এই উদাহরণে দেখানো হয়েছে কীভাবে SharedPreferences ব্যবহার করে লগইন তথ্য (নেম, পাসওয়ার্ড) মনে রাখা যায় "Remember me" চেকবক্সের সাথে।
প্রথম ধাপ: প্রজেক্ট তৈরি করুন
Android Studio-তে নতুন প্রজেক্ট তৈরি করুন।
দ্বিতীয় ধাপ: লেআউট ফাইল
res/layout/activity_main.xml - দুটি EditText, একটি বাটন এবং একটি চেকবক্স:
<?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"
tools:context=".MainActivity">
<EditText
android:id="@+id/etName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:ems="10"
android:layout_marginTop="75dp"
android:hint="Enter Name"/>
<EditText
android:id="@+id/etPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/etName"
android:ems="10"
android:layout_centerHorizontal="true"
android:hint="Enter Password"/>
<Button
android:id="@+id/btnLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:layout_below="@id/etPassword"
android:layout_alignStart="@id/etPassword"
android:layout_marginTop="10dp" />
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/btnLogin"
android:layout_marginTop="10dp"
android:text="Remember my credentials"
android:layout_alignStart="@id/btnLogin"/>
</RelativeLayout>
তৃতীয় ধাপ: স্ট্রিং রিসোর্স ফাইল
res/values/strings.xml - কী-গুলো সংজ্ঞায়িত করুন:
<resources>
<string name="app_name">Sample</string>
<string name="checkBox">Sample.checkbox</string>
<string name="name">Sample.name</string>
<string name="password">Sample.password</string>
</resources>
চতুর্থ ধাপ: মেইন এক্টিভিটিতে SharedPreferences লজিক
src/MainActivity.java:
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
EditText name, password;
Button button;
CheckBox checkBox;
String strName, strPassword, strCheckBox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = findViewById(R.id.etName);
password = findViewById(R.id.etPassword);
button = findViewById(R.id.btnLogin);
checkBox = findViewById(R.id.checkBox);
// Default SharedPreferences ইনিশিয়ালাইজ
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
editor = sharedPreferences.edit();
// সেভ করা ডেটা লোড করুন
checkSharedPreference();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (checkBox.isChecked()) {
// চেক করা থাকলে ডেটা সংরক্ষণ
editor.putString(getString(R.string.checkBox), "True");
editor.putString(getString(R.string.name), name.getText().toString());
editor.putString(getString(R.string.password), password.getText().toString());
editor.apply(); // commit() বদলে apply() ব্যবহার করুন (অ্যাসিনক্রোনাস)
} else {
// আনচেক থাকলে ডেটা মুছুন
editor.putString(getString(R.string.checkBox), "False");
editor.putString(getString(R.string.name), "");
editor.putString(getString(R.string.password), "");
editor.apply();
}
}
});
}
private void checkSharedPreference() {
strCheckBox = sharedPreferences.getString(getString(R.string.checkBox), "False");
strName = sharedPreferences.getString(getString(R.string.name), "");
strPassword = sharedPreferences.getString(getString(R.string.password), "");
name.setText(strName);
password.setText(strPassword);
checkBox.setChecked(strCheckBox.equals("True"));
}
}
পঞ্চম ধাপ: ম্যানিফেস্ট ফাইল
<?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>
গুরুত্বপূর্ণ টিপস
editor.commit()সিঙ্ক্রোনাস (UI থ্রেড ব্লক করে),editor.apply()অ্যাসিনক্রোনাস - apply() ব্যবহার করুন- সেনসিটিভ ডেটা (পাসওয়ার্ড, টোকেন) SharedPreferences-এ সংরক্ষণ করবেন না - EncryptedSharedPreferences বা Keystore ব্যবহার করুন
- বড় ডেটার জন্য Room Database বা File Storage ব্যবহার করুন
অ্যাপ্লিকেশন চালান
নেম/পাসওয়ার্ড লিখে "Remember me" চেক করে লগইন দিন। অ্যাপ রিস্টার্ট করলে ডেটা মনে থাকবে।
