chamamo
chamamo

Reputation: 280

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.

Upvotes: 2

Views: 1272

Answers (2)

marcel
marcel

Reputation: 313

(ConfigurationChanges = ConfigChanges.Orientation ) is enough;

Upvotes: 2

chamamo
chamamo

Reputation: 280

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

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

it work fine now

Upvotes: 7

Related Questions