[mono-android] Threading with Rotation

Jonathan Pryor jonp at xamarin.com
Tue Dec 13 22:31:41 EST 2011


On Nov 16, 2011, at 1:10 PM, emalamisura wrote:
> A common issue with maintaining state is handling Rotation with multiple
> threads in Android.  For instance if you start a thread, and then rotate the
> phone before the thread is done with its work Android destroys the previous
> Activity and generates a new one.  Any references you had to your previous
> activity will cause a reference exception.

Right. Don't do that. :-)

I'm also not sure what scenario you're thinking of. Activities are tied to their Main thread -- it isn't safe to interact with them except via the main thread or Activity.RunOnUiThread() -- and anything associated with the Activity follows the same rules as well. This would include the LastNonConfigurationInstance value, as used at:

	http://stackoverflow.com/a/8331994/83444

So the primary question is this: why are multiple threads using an Activity instance? If it's to use Activity.RunOnUiThread(), use an alternative, such as a Handler instance over the MainLooper:

	http://support.xamarin.com/customer/portal/questions/34836-toast-in-service

> This becomes even more difficult
> to handle in MonoDroid because alot of the semantics used in Java such as
> using an internal static class inside your activity can't easily be
> reproduced in C#

I also don't understand this. Java's `static` nested classes are ~identical to C# nested types; it's non-`static` nested classes which differ.

Thanks,
 - Jon



More information about the Monodroid mailing list