Search code examples
c#androidandroid-fragmentsxamarinmvvmcross

Xamarin Android FrameLayout displays blank/empty fragment on back where expected to close the app


I am implementing Fragments and hamburger, however when I click the back button I noticed the content_frame (which is filled by fragment) is always empty on the last 'back' pressed (where I expected to close the app instead of showing empty content).

Below is my FirstView.axml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:local="http://schemas.android.com/apk/res-auto"
   android:id="@+id/drawer_layout"
   android:layout_height="match_parent"
   android:layout_width="match_parent">
   <android.support.design.widget.CoordinatorLayout
       android:id="@+id/main_frame"
       android:fitsSystemWindows="true"
       android:layout_width="match_parent"
       android:layout_height="match_parent">
       <FrameLayout
           android:id="@+id/content_frame"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:layout_centerInParent="true" />
   </android.support.design.widget.CoordinatorLayout>
   <FrameLayout
       android:id="@+id/navigation_frame"
       android:layout_height="match_parent"
       android:layout_width="wrap_content"
       android:layout_gravity="left|start" />

Here is the OnBackPressed from my activity

   public override void OnBackPressed()
   {
       if (DrawerLayout != null && DrawerLayout.IsDrawerOpen(GravityCompat.Start))
           DrawerLayout.CloseDrawers();
       else
           base.OnBackPressed();
   }

To navigate between fragment, I use navigate function from my MenuFragment

private async Task Navigate(int itemId)
   {
       ((FirstView)Activity).DrawerLayout.CloseDrawers();
       // add a small delay to prevent any UI issues
       await Task.Delay(TimeSpan.FromMilliseconds(250));

       switch (itemId)
       {
           case Resource.Id.nav_input:
               ViewModel.ShowInputCommand.Execute();
               break;
           case Resource.Id.nav_registercc:
               ViewModel.ShowRegisterCCCommand.Execute();
               break;
           case Resource.Id.nav_creditcard:
               ViewModel.ShowCreditCardCommand.Execute();
               break;
           case Resource.Id.nav_transaction:
               ViewModel.ShowTransactionCommand.Execute();
               break;
       }
    }

Basically, the issue that I am facing is instead of closing the app after content_frame showing the initial fragment, it displays empty/blank fragment (navigation_frame still working). And as you can see, I am following everything from this MvvmCross tutorial.

Any help would be appreciated.


Solution

  • I just realize in my initial fragment the header should be [MvxFragment(typeof(FirstViewModel), Resource.Id.content_frame)] instead of [MvxFragment(typeof(FirstViewModel), Resource.Id.content_frame, true)]. It is now working great as expected!