In CommonsWare eBook Android v3.6 pg 270, he discusses handling threads with rotation. He offers a solution where you create an inner static class to hold your state, call detach in the workflow and then attach again during screen rotation.
The problem is that each rotation will destroy the Activity and recreate it in Android, thus when you come back your thread may reference the destroyed activity giving you an exception for accessing an object set for collection.
I tried this in Mono Android and was unable to get it to work, I got an exception every single time. My question, hopefully Jonathan Pryer reads this is, how can I make this work in Mono Android? I have asked this twice on the forums with no results. So I am taking it to StackOverflow. I wanted to post the actualy code but I didn't want to violate CommonsWare licensing. So please take a look at the example in the book.
What was the exception? What's the
adb logcat
output?The C# equivalent is the-same-but-different from the Java source. For example, here's the "rotation-aware" version of the default Mono for Android sample project:
It's the same basic approach as the Java sample: the Activity.OnRetainNonConfigurationInstance() method is invoked by Android before the Activity is disposed, so we return our "state" from that method. The Activity.LastNonConfigurationInstance property will return
null
the first time it's invoked, otherwise it will return the last value returned fromOnRetainNonConfigurationInstance()
. The only missing piece of the puzzle is that the "state" object (CountInfo
here) must inherit from Java.Lang.Object as we're passing the instance to Java.