Search code examples
androidxamarinmvvmcrossandroid-toolbar

Actionbar lost after rotation in dialog with toolbar


I'm implementing an app in Xamarin Android that contains a page that once you click on an actionbar button, you get a new dialog that contains a toolbar.

The simplified code is something like:

public class MyDialogFragment : MvxDialogFragment<MyDialogViewModel>
    {
        public MvxDialogFragment()
        {
        }

        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            this.EnsureBindingContextIsSet(inflater);

            var view = this.BindingInflate(Resource.Layout.dialog_view, null);

            SetupToolbar(view);

            return view;
        }

        private void SetupToolbar(View view)
        {
            Dialog.RequestWindowFeature((int)WindowFeatures.NoTitle);

            Android.Support.V7.Widget.Toolbar toolbar = view.FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.dialog_toolbar);
            ((AppCompatActivity)Activity).SetSupportActionBar(toolbar);

            Android.Support.V7.App.ActionBar actionBar = ((AppCompatActivity)Activity).SupportActionBar;
            actionBar.Title = null;

            HasOptionsMenu = true;
        }

        public override void OnCreateOptionsMenu(IMenu menu, MenuInflater inflater)
        {
            menu.Clear();
            inflater.Inflate(Resource.Menu.menu_dialog, menu);
            base.OnCreateOptionsMenu(menu, inflater);
        }

        public override bool OnOptionsItemSelected(IMenuItem item)
        {
            switch (item.ItemId)
            {
                (...)
            }
        }

        public override void OnPrepareOptionsMenu(IMenu menu)
        {
          (...)

            base.OnPrepareOptionsMenu(menu);
        }

The main workflow it works just fine, but there is a side effect when I rotate the screen while displaying the dialog. If I do so, the actionbar buttons of the parent fragment(the one hosting the dialog), disappear till I recreate the view (ie: rotation).

Any ideas about how to solve this? I have tried several things like invalidate the parent menu after the dialog is closed, but it didn't work.


Solution

  • After some time investigating the problem, I don't think there is a way to make work 2 actionbars at the same time in a "native way".

    My solution, in the end, was to manually handle the click events over the second actionbar.

    public class MyDialogFragment : MvxDialogFragment<MyDialogViewModel>
        {
            public MvxDialogFragment()
            {
            }
    
            public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
            {
                this.EnsureBindingContextIsSet(inflater);
    
                var view = this.BindingInflate(Resource.Layout.dialog_view, null);
    
                SetupToolbar(view);
    
                return view;
            }
    
            private void SetupToolbar(View view)
            {
                Dialog.RequestWindowFeature((int)WindowFeatures.NoTitle);
    
            toolbar = view.FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
            toolbar.Title = null;
    
            toolbar.InflateMenu(Resource.Menu.menu);
            toolbar.MenuItemClick += HandlerMenuItemClick;
    
            HasOptionsMenu = true;
            }
    
            public override bool HandlerMenuItemClick(object sender, Android.Support.V7.Widget.Toolbar.MenuItemClickEventArgs e)
            {
                switch (e.item.ItemId)
                {
                    (...)
                }
            }
    
    }
    

    Hope it can helps anybody