O'Reilly logo

Xamarin Mobile Development for Android Cookbook by Matthew Leibowitz

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required