Search code examples
c#androidmvvmcross

MvvmCross Android: Binding lost after screen rotation


My binding work fine, but after screen rotation I loose binding.

In OnCreateView I try to restore fragment by Tag, if FindFragmentByTag return null I create a new instance like this:

protected virtual TFragmentType PutFragment<TFragmentType, TViewModelType>(int oldId, TViewModelType vm)
        where TFragmentType : FragmentBase, new()
        where TViewModelType : IViewModelBase
    {
        var tag = typeof(TFragmentType).FullName;
        var fragment = SupportFragmentManager.FindFragmentByTag(tag) as TFragmentType;
        if (fragment == null)
        {
            fragment = new TFragmentType() { ViewModel = vm };
            SupportFragmentManager.BeginTransaction().Replace(oldId, fragment, tag).Commit();
            fragment.RetainInstance = true;
        }
        return fragment;
    }

The probleme occur when GetFragmentByTag return some thing (in my case after sctreen rotation).

ScreenShots

Before rotation:

Normal

After:

Empty

Thank you in advance.


Solution

  • Thank you guys, I specified "ConfigurationChanges" in the activity attribute

    [Activity (ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.ScreenSize | ConfigChanges.KeyboardHidden)]
    

    it work fine now