Share via


ISharedPreferences Interface

Definition

Interface for accessing and modifying preference data returned by Context#getSharedPreferences.

[Android.Runtime.Register("android/content/SharedPreferences", "", "Android.Content.ISharedPreferencesInvoker")]
public interface ISharedPreferences : Android.Runtime.IJavaObject, IDisposable, Java.Interop.IJavaPeerable
[<Android.Runtime.Register("android/content/SharedPreferences", "", "Android.Content.ISharedPreferencesInvoker")>]
type ISharedPreferences = interface
    interface IJavaObject
    interface IDisposable
    interface IJavaPeerable
Attributes
Implements

Remarks

Interface for accessing and modifying preference data returned by Context#getSharedPreferences. For any particular set of preferences, there is a single instance of this class that all clients share. Modifications to the preferences must go through an Editor object to ensure the preference values remain in a consistent state and control when they are committed to storage. Objects that are returned from the various get methods must be treated as immutable by the application.

SharedPreferences is best suited to storing data about how the user prefers to experience the app, for example, whether the user prefers a particular UI theme or whether they prefer viewing particular content in a list vs. a grid. To this end, SharedPreferences reflects changes Editor#commit() committed or Editor#apply() applied by Editors <em>immediately</em>, potentially before those changes are durably persisted. Under some circumstances such as app crashes or termination these changes may be lost, even if an OnSharedPreferenceChangeListener reported the change was successful. SharedPreferences is not recommended for storing data that is sensitive to this kind of rollback to a prior state such as user security or privacy settings. For other high-level data persistence options, see Room or DataStore.

<em>Note:</em> Common implementations guarantee that outstanding edits to preference files are persisted to disk when host Activities become stopped. In some situations (e.g. performing many Editor#commit() or Editor#apply() operations just prior to navigating away from the host Activity) this can lead to blocking the main thread during lifecycle transition events and associated ANR errors. For more details see the documentation for Editor#commit() and Editor#apply().

<em>Note: This class does not support use across multiple processes.</em>

<div class="special reference"> <h3>Developer Guides</h3>

For more information about using SharedPreferences, read the Data Storage developer guide.

</div>

Java documentation for android.content.SharedPreferences.

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Properties

All

Retrieve all values from the preferences.

Handle

Gets the JNI value of the underlying Android object.

(Inherited from IJavaObject)
JniIdentityHashCode

Returns the value of java.lang.System.identityHashCode() for the wrapped instance.

(Inherited from IJavaPeerable)
JniManagedPeerState

State of the managed peer.

(Inherited from IJavaPeerable)
JniPeerMembers

Member access and invocation support.

(Inherited from IJavaPeerable)
PeerReference

Returns a JniObjectReference of the wrapped Java object instance.

(Inherited from IJavaPeerable)

Methods

Contains(String)

Checks whether the preferences contains a preference.

Disposed()

Called when the instance has been disposed.

(Inherited from IJavaPeerable)
DisposeUnlessReferenced()

If there are no outstanding references to this instance, then calls Dispose(); otherwise, does nothing.

(Inherited from IJavaPeerable)
Edit()

Create a new Editor for these preferences, through which you can make modifications to the data in the preferences and atomically commit those changes back to the SharedPreferences object.

Finalized()

Called when the instance has been finalized.

(Inherited from IJavaPeerable)
GetBoolean(String, Boolean)

Retrieve a boolean value from the preferences.

GetFloat(String, Single)

Retrieve a float value from the preferences.

GetInt(String, Int32)

Retrieve an int value from the preferences.

GetLong(String, Int64)

Retrieve a long value from the preferences.

GetString(String, String)

Retrieve a String value from the preferences.

GetStringSet(String, ICollection<String>)

Retrieve a set of String values from the preferences.

RegisterOnSharedPreferenceChangeListener(ISharedPreferencesOnSharedPreferenceChangeListener)

Registers a callback to be invoked when a change happens to a preference.

SetJniIdentityHashCode(Int32)

Set the value returned by JniIdentityHashCode.

(Inherited from IJavaPeerable)
SetJniManagedPeerState(JniManagedPeerStates) (Inherited from IJavaPeerable)
SetPeerReference(JniObjectReference)

Set the value returned by PeerReference.

(Inherited from IJavaPeerable)
UnregisterFromRuntime()

Unregister this instance so that the runtime will not return it from future Java.Interop.JniRuntime+JniValueManager.PeekValue invocations.

(Inherited from IJavaPeerable)
UnregisterOnSharedPreferenceChangeListener(ISharedPreferencesOnSharedPreferenceChangeListener)

Unregisters a previous callback.

Extension Methods

JavaCast<TResult>(IJavaObject)

Performs an Android runtime-checked type conversion.

JavaCast<TResult>(IJavaObject)
GetJniTypeName(IJavaPeerable)

Gets the JNI name of the type of the instance self.

JavaAs<TResult>(IJavaPeerable)

Try to coerce self to type TResult, checking that the coercion is valid on the Java side.

TryJavaCast<TResult>(IJavaPeerable, TResult)

Try to coerce self to type TResult, checking that the coercion is valid on the Java side.

Applies to