সেভ করা পৃষ্ঠা গুছিয়ে রাখতে 'সংগ্রহ' ব্যবহার করুন
আপনার পছন্দ অনুযায়ী কন্টেন্ট সেভ করুন ও সঠিক বিভাগে রাখুন।
যদি আপনার কাছে কী-মানের তুলনামূলকভাবে ছোট সংগ্রহ থাকে যা আপনি সংরক্ষণ করতে চান, আপনি SharedPreferences API ব্যবহার করতে পারেন। একটি SharedPreferences অবজেক্ট কী-মান জোড়া ধারণকারী একটি ফাইলের দিকে নির্দেশ করে এবং সেগুলি পড়তে এবং লিখতে সহজ পদ্ধতি প্রদান করে। প্রতিটি SharedPreferences ফাইল ফ্রেমওয়ার্ক দ্বারা পরিচালিত হয় এবং ব্যক্তিগত বা শেয়ার করা যেতে পারে।
এই পৃষ্ঠাটি আপনাকে দেখায় কিভাবে সাধারণ মানগুলি সংরক্ষণ এবং পুনরুদ্ধার করতে SharedPreferences API ব্যবহার করতে হয়।
ভাগ করা পছন্দগুলির একটি হ্যান্ডেল পান৷
আপনি একটি নতুন ভাগ করা পছন্দ ফাইল তৈরি করতে পারেন বা এই পদ্ধতিগুলির মধ্যে একটিতে কল করে বিদ্যমান একটি অ্যাক্সেস করতে পারেন:
getSharedPreferences() : যদি আপনার নাম দ্বারা চিহ্নিত একাধিক ভাগ করা পছন্দের ফাইলের প্রয়োজন হয় তবে এটি ব্যবহার করুন, যা আপনি প্রথম প্যারামিটার দিয়ে নির্দিষ্ট করেছেন। আপনি আপনার অ্যাপের যেকোনো Context থেকে এটি কল করতে পারেন।
getPreferences() : আপনি যদি ক্রিয়াকলাপের জন্য শুধুমাত্র একটি ভাগ করা পছন্দ ফাইল ব্যবহার করতে চান তবে একটি Activity থেকে এটি ব্যবহার করুন। যেহেতু এটি একটি ডিফল্ট ভাগ করা পছন্দ ফাইল পুনরুদ্ধার করে যা কার্যকলাপের অন্তর্গত, আপনাকে একটি নাম সরবরাহ করতে হবে না।
উদাহরণস্বরূপ, নিম্নলিখিত কোডটি শেয়ার করা পছন্দের ফাইল অ্যাক্সেস করে যা রিসোর্স স্ট্রিং R.string.preference_file_key দ্বারা চিহ্নিত করা হয় এবং এটি ব্যক্তিগত মোড ব্যবহার করে খোলে যাতে ফাইলটি শুধুমাত্র আপনার অ্যাপ দ্বারা অ্যাক্সেসযোগ্য হয়:
আপনার ভাগ করা পছন্দের ফাইলগুলির নামকরণ করার সময়, আপনার এমন একটি নাম ব্যবহার করা উচিত যা আপনার অ্যাপে অনন্যভাবে শনাক্তযোগ্য। এটি করার একটি ভাল উপায় হল আপনার অ্যাপ্লিকেশন আইডির সাথে ফাইলের নামের উপসর্গ। যেমন: "com.example.myapp.PREFERENCE_FILE_KEY"
বিকল্পভাবে, আপনার কার্যকলাপের জন্য যদি আপনার শুধুমাত্র একটি ভাগ করা পছন্দের ফাইলের প্রয়োজন হয়, আপনি getPreferences() পদ্ধতি ব্যবহার করতে পারেন:
আপনি যদি অ্যাপ সেটিংস সংরক্ষণ করতে SharedPreferences API ব্যবহার করেন, তাহলে আপনার পুরো অ্যাপের জন্য ডিফল্ট শেয়ার করা পছন্দ ফাইল পেতে আপনার পরিবর্তে getDefaultSharedPreferences() ব্যবহার করা উচিত। আরও তথ্যের জন্য, সেটিংস বিকাশকারী নির্দেশিকা দেখুন।
apply() ইন-মেমরি SharedPreferences অবজেক্ট অবিলম্বে পরিবর্তন করে কিন্তু অসিঙ্ক্রোনাসভাবে ডিস্কে আপডেট লেখে। বিকল্পভাবে, সিঙ্ক্রোনাসভাবে ডিস্কে ডেটা লিখতে আপনি commit() ব্যবহার করতে পারেন। কিন্তু যেহেতু commit() সিঙ্ক্রোনাস, তাই আপনার মূল থ্রেড থেকে এটিকে কল করা এড়াতে হবে কারণ এটি আপনার UI রেন্ডারিংকে থামাতে পারে।
ভাগ করা পছন্দ থেকে পড়ুন
একটি ভাগ করা পছন্দের ফাইল থেকে মান পুনরুদ্ধার করতে, কল পদ্ধতি যেমন getInt() এবং getString() , আপনি যে মানটি চান তার জন্য কী প্রদান করুন এবং কী উপস্থিত না থাকলে ঐচ্ছিকভাবে একটি ডিফল্ট মান ফেরত দিন। যেমন:
এই পৃষ্ঠার কন্টেন্ট ও কোডের নমুনাগুলি Content License-এ বর্ণিত লাইসেন্সের অধীনস্থ। Java এবং OpenJDK হল Oracle এবং/অথবা তার অ্যাফিলিয়েট সংস্থার রেজিস্টার্ড ট্রেডমার্ক।
2025-07-29 UTC-তে শেষবার আপডেট করা হয়েছে।
[[["সহজে বোঝা যায়","easyToUnderstand","thumb-up"],["আমার সমস্যার সমাধান হয়েছে","solvedMyProblem","thumb-up"],["অন্যান্য","otherUp","thumb-up"]],[["এতে আমার প্রয়োজনীয় তথ্য নেই","missingTheInformationINeed","thumb-down"],["খুব জটিল / অনেক ধাপ","tooComplicatedTooManySteps","thumb-down"],["পুরনো","outOfDate","thumb-down"],["অনুবাদ সংক্রান্ত সমস্যা","translationIssue","thumb-down"],["নমুনা / কোড সংক্রান্ত সমস্যা","samplesCodeIssue","thumb-down"],["অন্যান্য","otherDown","thumb-down"]],["2025-07-29 UTC-তে শেষবার আপডেট করা হয়েছে।"],[],[],null,["# Save simple data with SharedPreferences\n\nIf you have a relatively small collection of key-values that you'd like to save,\nyou can use the [`SharedPreferences`](/reference/android/content/SharedPreferences) APIs. A `SharedPreferences` object\npoints to a file containing key-value pairs and provides simple methods to read\nand write them. Each `SharedPreferences` file is managed by the framework and\ncan be private or shared.\n\nThis page shows you how to use the `SharedPreferences` APIs to store and\nretrieve simple values.\n| **Caution:** `DataStore` is a modern data storage solution that you should use instead of `SharedPreferences`. It builds on Kotlin coroutines and Flow, and overcomes many of the drawbacks of `SharedPreferences`.\n|\n| Read the [DataStore guide](/topic/libraries/architecture/datastore) for more information.\n| **Note:** The `SharedPreferences` APIs are for reading and writing key-value pairs, and you shouldn't confuse them with the [`Preference`](/reference/android/preference/Preference) APIs, which help you build a user interface for your app settings (although they also use `SharedPreferences` to save the user's settings). For information about the [`Preference`](/reference/android/preference/Preference) APIs, see the [Settings developer guide](/guide/topics/ui/settings).\n\nGet a handle to shared preferences\n----------------------------------\n\nYou can create a new shared preference file or access an existing one by calling\none of these methods:\n\n- **[`getSharedPreferences()`](/reference/android/content/Context#getSharedPreferences(java.lang.String,%20int)):** Use this if you need multiple shared preference files identified by name, which you specify with the first parameter. You can call this from any [`Context`](/training/data-storage/shared-preferences#kotlin:%7E:text=this%20from%20any-,Context,-in%20your%20app) in your app.\n- **[`getPreferences()`](/reference/android/app/Activity#getPreferences(int)):** Use this from an [`Activity`](/reference/android/app/Activity) if you need to use only one shared preference file for the activity. Because this retrieves a default shared preference file that belongs to the activity, you don't need to supply a name.\n\nFor example, the following code accesses the shared preferences file that's\nidentified by the resource string `R.string.preference_file_key` and opens it\nusing the private mode so the file is accessible by only your app: \n\n### Kotlin\n\n```kotlin\nval sharedPref = activity?.getSharedPreferences(\n getString(R.string.preference_file_key), Context.MODE_PRIVATE)\n```\n\n### Java\n\n```java\nContext context = getActivity();\nSharedPreferences sharedPref = context.getSharedPreferences(\n getString(R.string.preference_file_key), Context.MODE_PRIVATE);\n```\n\nWhen naming your shared preference files, you should use a name that's uniquely\nidentifiable to your app. A good way to do this is prefix the file name with\nyour [application ID](/studio/build/configure-app-module#set_the_application_id). For example:\n`\"com.example.myapp.PREFERENCE_FILE_KEY\"`\n\nAlternatively, if you need just one shared preference file for your activity,\nyou can use the [`getPreferences()`](/reference/android/app/Activity#getPreferences(int)) method: \n\n### Kotlin\n\n```kotlin\nval sharedPref = activity?.getPreferences(Context.MODE_PRIVATE)\n```\n\n### Java\n\n```java\nSharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);\n```\n| **Caution:** The [`MODE_WORLD_READABLE`](/reference/android/content/Context#MODE_WORLD_READABLE) and [`MODE_WORLD_WRITEABLE`](/reference/android/content/Context#MODE_WORLD_WRITEABLE) modes have been deprecated since API level 17.\n|\n| Starting with Android 7.0 (API level 24), Android throws a\n| [`SecurityException`](/reference/java/lang/SecurityException) if you use them. If your app needs to share private\n| files with other apps, it may use a [`FileProvider`](/reference/androidx/core/content/FileProvider) with the\n| [`FLAG_GRANT_READ_URI_PERMISSION`](/reference/android/content/Intent#FLAG_GRANT_READ_URI_PERMISSION). For more information, also see\n| [Sharing Files](/training/secure-file-sharing).\n\nIf you're using the `SharedPreferences` API to save app settings, you\nshould instead use [`getDefaultSharedPreferences()`](/reference/android/preference/PreferenceManager#getDefaultSharedPreferences(android.content.Context)) to get the default\nshared preference file for your entire app. For more information, see the\n[Settings developer guide](/guide/topics/ui/settings).\n\nWrite to shared preferences\n---------------------------\n\nTo write to a shared preferences file, create a [`SharedPreferences.Editor`](/reference/android/content/SharedPreferences.Editor)\nby calling [`edit()`](/reference/android/content/SharedPreferences#edit()) on your `SharedPreferences`.\n\nPass the keys and values you want to write with methods such as:\n[`putInt()`](/reference/android/content/SharedPreferences.Editor#putInt()) and[`putString()`](/reference/android/content/SharedPreferences.Editor#putString()). Then call [`apply()`](/reference/android/content/SharedPreferences.Editor#apply()) or\n[`commit()`](/reference/android/content/SharedPreferences.Editor#commit()) to save the changes. For example: \n\n### Kotlin\n\n```kotlin\nval sharedPref = activity?.getPreferences(Context.MODE_PRIVATE) ?: return\nwith (sharedPref.edit()) {\n putInt(getString(R.string.saved_high_score_key), newHighScore)\n apply()\n}\n```\n\n### Java\n\n```java\nSharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);\nSharedPreferences.Editor editor = sharedPref.edit();\neditor.putInt(getString(R.string.saved_high_score_key), newHighScore);\neditor.apply();\n```\n\n`apply()` changes the in-memory `SharedPreferences` object immediately but\nwrites the updates to disk asynchronously. Alternatively, you can use `commit()`\nto write the data to disk synchronously. But because `commit()` is synchronous,\nyou should avoid calling it from your main thread because it could pause your UI\nrendering.\n\nRead from shared preferences\n----------------------------\n\nTo retrieve values from a shared preferences file, call methods such as\n[`getInt()`](/reference/android/content/SharedPreferences#getInt(java.lang.String,%20int)) and [`getString()`](/reference/android/content/SharedPreferences#getString(java.lang.String,%20java.lang.String)), providing the key for the value you\nwant, and optionally a default value to return if the key isn't present. For\nexample: \n\n### Kotlin\n\n```kotlin\nval sharedPref = activity?.getPreferences(Context.MODE_PRIVATE) ?: return\nval defaultValue = resources.getInteger(R.integer.saved_high_score_default_key)\nval highScore = sharedPref.getInt(getString(R.string.saved_high_score_key), defaultValue)\n```\n\n### Java\n\n```java\nSharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);\nint defaultValue = getResources().getInteger(R.integer.saved_high_score_default_key);\nint highScore = sharedPref.getInt(getString(R.string.saved_high_score_key), defaultValue);\n```"]]