Timo
Timo

Reputation: 65

What's the proper way to implement Fragments in MvvmCross 4.1.4

I am looking for an example or documentation to work with fragments in the current mvvmcross version. I found a lot of information about the old versions but I am confused how I should implement it now.

The best information so far I have found is in this article: http://gregshackles.com/presenters-in-mvvmcross-navigating-android-with-fragments/

But it is over one year old and I think there is already a newer/better way to do it.

What I want is having a single Activity(like in an SPA or having a MainView) this activity should contain one or two fragment and on a button click I want to navigate/change to a different fragment and the current one on the backstack for navigation. Or is it recommend to use only activities for navigation and not implementing a SPA?

Thanks for your help! Timo

Upvotes: 6

Views: 4720

Answers (1)

Martijn00
Martijn00

Reputation: 3559

The new way to use fragments in MvvmCross is by using attributes on your fragment.

[MvxFragment(typeof(ActivityHostViewModel), Resource.Id.content_frame, true)]
public class HomeFragment : BaseFragment<HomeViewModel>
{
}

The MvvmCross presenter will recognize that this is a fragment, and show it inside the host activity which is attached to the host viewmodel.

To support fragments in the presenter you need to add the following to your setup.cs

protected override IMvxAndroidViewPresenter CreateViewPresenter()
{
    var mvxFragmentsPresenter = new MvxFragmentsPresenter(AndroidViewAssemblies);
    Mvx.RegisterSingleton<IMvxAndroidViewPresenter>(mvxFragmentsPresenter);
    return mvxFragmentsPresenter;
}

A full sample is available here: https://github.com/MvvmCross/MvvmCross/tree/develop/TestProjects/Android-Support/Fragments

Upvotes: 15

Related Questions