Preserving view and fragment state
It is important to save the state of a view, fragment, or activity. Each has a limited lifetime and may be stopped at any point, such as when the user closes the activity, navigates away from, or even rotates the device.
How to do it...
In order to preserve the state across certain actions, such as when the user switches between apps, we should save the state to the instance state parcel. Let's take a look at the following steps:
Saving state for a view is done by inheriting from a type that implements the
IParcelable
interface, such asBaseSavedState
:private class InstanceState : BaseSavedState { public InstanceState(IParcelable superState) : base(superState) { } public InstanceState(Parcel parcel) : base(parcel) { Interval = parcel.ReadInt(); } public int Interval { get; set; } public override void WriteToParcel( Parcel dest, ParcelableWriteFlags flags) { base.WriteToParcel(dest, flags); dest.WriteInt(Interval); ...