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:

  1. Saving state for a view is done by inheriting from a type that implements the IParcelable interface, such as BaseSavedState:
    private class InstanceState : BaseSavedState { public InstanceState(IParcelable superState) : base(superState) { } public InstanceState(Parcel parcel) : base(parcel) ...

Get Xamarin Mobile Development for Android Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.