I am unsure if this is a general Andorid or a Xamarin-specific question. I have an Activity with separate layouts for landscape and portrait configuration. The portrait layout has a widget that should be animated after the OK button is pressed, but the landscape view does not have this widget.
In my activity, the event handler for the OK button has the event handler
private void handleOkEvent() {
if (isLandscapeMode()) {
return;
}
Widget.StartAnimation();
...
}
...and I also have the following supporting code:
private bool isLandscapeMode() {
return WindowManager.DefaultDisplay.Rotation == SurfaceOrientation.Rotation90
|| WindowManager.DefaultDisplay.Rotation == SurfaceOrientation.Rotation270;
}
private ImageView Widget
{
get
{
return FindViewById<ImageView>(Resource.Id.widget);
}
}
However, this creates a narrow error situation. If the user is in landscape mode, hits OK and immediately flips to portrait mode, isLandscapeMode returns true but the Widget property returns null since the Widget has not yet been created (we are not fully in portrait mode yet, but isLandscapeMode says we are). Hence Widget.StartAnimation() causes a NullReferenceException.
I can easily check whether the Widget property returns null, but this seems like a hack and would be painful for landscape & portrait layouts that have greater differences than in this example. Am I using the Android Activity correctly here, or is there a more elegant way of doing this?
Or option B, does this appear to be a Xamarin-specific issue that would not be a problem on vanilla Android?
Aucun commentaire:
Enregistrer un commentaire