Book Image

Xamarin Mobile Application Development for Android

Book Image

Xamarin Mobile Application Development for Android

Overview of this book

Table of Contents (18 chapters)
Xamarin Mobile Application Development for Android Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Handling configuration changes with fragments


In Chapter 6, Making Your App Orientation-aware, we discussed how Android activities respond to device configuration changes. In this section, we will take a look at the fragment behavior in the event of configuration changes.

Like activities, fragment provides the OnSaveInstanceState() method that can be used to save the state of your fragment. This works in pretty much the same way as its activity counterpart. The OnSaveInstanceState() method supplies a Bundle instance into which we can dump the data bundles. The saved fragment instance can be retrieved from the OnCreate(), OnActivityCreated(), or OnCreateView() callback.

The following steps will guide you through saving and retaining the POI list scroll position when the device configuration changes:

  1. Get the index of the first visible list item and save it to a bundle in the OnSaveInstanceState() method:

    public override void OnSaveInstanceState (Bundle outState)
    {
      base.OnSaveInstanceState ...