So I have a problem with a fragment not showing inside a MvxCachingFragmentCompatActivity
.
The pattern I use to get to the problem is as follows:
await _navigationService.Navigate<[TheFragmentViewModel]>();
Fragment declaration:
[MvxFragment(typeof(MainViewModel), Resource.Id.content_frame, true)]
[Register(nameof(FirstFragment))]
public class FirstFragment : MvxFragment<FirstViewModel>
{
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
base.OnCreateView(inflater, container, savedInstanceState);
var view = this.BindingInflate(Resource.Layout.FirstView, container, false);
return view;
}
}
Main activity: (nothing special I think)
[Activity(Label = "Fragment View")]
public class MainActivity : MvxCachingFragmentCompatActivity<MainViewModel>
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.MainView);
}
}
Main viewmodel
public class MainViewModel : MvxViewModel
{
private readonly IMvxNavigationService _navigationService;
public MainViewModel(IMvxNavigationService navigationService)
{
_navigationService = navigationService;
Init();
}
public async void Init()
{
await _navigationService.Navigate<FirstViewModel>();
}
}
Main activity layout: (very simple layout)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
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" />
</LinearLayout>
I also added a sample on github: Github link.
I also added a bug report on the Mvvmcross github, but I am not sure if it is a bug on my part or theirs?
You should never use async void
or start an async task from a non async command. These are the first problems. Also your Initialize is not called because you are not using RegisterNavigationServiceAppStart<>()
. Another thing is that you are supposed to navigate directly to a fragment and not first to the activity, because MvvmCross will handle that.
Another hint will be to use Dependency injection to resolve IMvxNavigationService
.