Search code examples
androidandroid-fragmentsxamarinmvvmcross

MVVMCROSS Fragment sample not working with rotation


I just started out with learning how to use MVVMCross with Android using Xamarin Studio. I am fairly new to Android development.

I am porting an iOS app to Android and when I rotate my screen my app crashes when using fragments. I thought the problem was just me doing something wrong but when I run the sample project attached to the sample video the same thing happens. The error i get is " No default constructor found".

The Sample app I used at github : https://github.com/MvvmCross/MvvmCross-Tutorials/tree/master/Fragments

Can someone please help me out to why this is happening ?


Solution

  • After some digging, I "think" you are asking about how to handle rotation for a DialogFragment - I hope I've got that right.

    If you do want to handle rotation in Android, then you have to:

    • provide the default constructor and you have to make use of the fragment tag
    • provide some code in the Activity.OnCreate handler in order to ensure that the DialogFragment has its ViewModel set after the rotation occurs.

    I've just done this for the sample you mention in this commit - https://github.com/MvvmCross/MvvmCross-Tutorials/commit/ebe1777ec79ee2607b1b863f6b8b3911c12df53f


    Note that to really fully support the Activity lifecycle - e.g. including loading/saving state when the App or Activity is "tombstoned" - then you should also go further than this - should save/restore all View and ViewModel state. MvvmCross does contain some helpers for this in the SaveState and ReloadState ViewModel methods - but even with these then fully supporting the Activity lifecycle can be quite a pain (which is why some apps don't fully support it, but instead show a "new" display instead)