Hello to all,
how do I save and restore an object (a class) at the time that the Activity is destroyed and recreated, such as when the device is rotated?
Generally speaking, objects aren't suitable candidates for instance state (unless they are pretty simple objects). Serializing them properly can be a real PITA.
In the case of configuration changes, you could use a fragment without a view, and set the RetainInstance property of the fragment to true. When you do this, the fragment will not be destroyed when the activity is torn down during a config change. Instead, the fragment will survive the config change. So, if you use the fragment as a container for your object, then your object will survive the config change as well.
I've got an example of this in the talk I gave at Xamarin Evolve back in April. You can find the code in Github here: https://github.com/topgenorth/evolve_fragments_the_next_step.
Look at the RetainStateActivity and the code for the fragment in the StateHolderFragment class.
Xamarin Inc., as a wholly-owned Microsoft subsidiary acting as a separate legal entity, adheres to the Microsoft Privacy Statement: Privacy & cookies