In Android when one rotates the screen, components like a Checkbox loose their state (except you consider this in your architecture by keeping the state in the ViewModel etc.). How is that in Xamarin.Forms and MAUI, where is the state kept? Do they suffer from these problems or is that automatically handled?
Asked
Active
Viewed 91 times
-1
-
I don't understand the question and its purpose a it's very vague. What do you mean by "on Android"? Generally, state is kept in ViewModels using the MVVM pattern and rotating the screen doesn't affect the ViewModels. – Julian Mar 19 '23 at 13:54
-
I asked for Android because I know that on native Android this issue exists. If rotating the screen does not affect the ViewModel, that's great. Is that true for both, Xamarin.Forms and MAUI? – stefan.at.kotlin Mar 19 '23 at 14:12
-
What do you mean by "native Android" (e.g. Java, Kotlin)? Xamarin.Forms and MAUI use native controls under the hood. The easiest way to find out would be to try it out yourself. Create a simple app, without MVVM, add some checkboxes to the main page, run the app, make a selection and rotate the screen. AFAIK, state should be persistent between screen orientations, but I cannot give a definitive answer on that. Generally, I think that individual controls shouldn't be responsible for maintaining state anyway, if possible. – Julian Mar 19 '23 at 15:35
1 Answers
0
For the native android, you can use the Activity's onSaveInstanceState(Bundle outState)
and onRestoreInstanceState(Bundle savedInstanceState)
to save the state when the screen rotared.
For the maui and xamarin, you can also override the two method in the MainActivity to save the UI state. You can refer to this case about What's the right way to save instance state only on rotation? and this one about Android save state on orientation change.
But I suggest you use the MVVM, because the UI state is binding to the data in the viewmodel. When user rotate the screen, only the Activity will recreate and the UI will redraw. The data in the viewmdoel will not lose.

Liyun Zhang - MSFT
- 8,271
- 1
- 2
- 14