Fragments in MVVMCross 4.1.4 will not be displayed in the Activity (Android)
Asked Answered
W

1

0

I try to add a fragment to an activity and have already looked at the example from Martijn00.

My problem is, that the activity will be displayed but the fragment will not and in debugging mode, the debugger is not running into the fragment's method OnCreateView. So the attribute of the fragment will be used, but not the fragment. I see only my activity with the green background. I am sure I do something wrong, but after 6 hours I do give up and ask you guys for help :)

Fragment:

namespace TopDownTodoList.Mobile.Droid.Views.Fragments{

[MvxFragment(typeof(MainViewModel), Resource.Id.content_frame, true)]
[Register("topdowntodolist.mobile.droid.views.fragments.TodoOverviewFragment")]
public class TodoOverviewFragment :  MvxFragment<TodoOverviewViewModel>
{
    protected int LayoutId => Resource.Layout.f_todo_overview_fragment;

    public override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        this.Init();
    }

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        return this.BindingInflate(this.LayoutId, null);
        //return inflater.Inflate(this.LayoutId, container, false);
    }

    public virtual void Init() { }
}}

Activity:

namespace TopDownTodoList.Mobile.Droid.Views{

[Activity(Label = "MainView")]
public class MainView : MvvmCross.Droid.FullFragging.Views.MvxActivity<MainViewModel>
{
    protected int LayoutId => Resource.Layout.main_view;

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        SetContentView(this.LayoutId);

        this.Init();
    }

    public virtual void Init(){}
}}

App.cs:

        protected override void RegisterClasses()
    {
        RegisterAppStart<TodoOverviewViewModel>();
    }

ViewModels:

public class VMName : MvvmCross.Core.ViewModels.MvxViewModel{...}

FragmentLayout (f_todo_overview_fragment):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:mvx="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:background="#0000aa"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_gravity="center"
    android:gravity="center"/></LinearLayout>

ActivityLayout (main_view):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:background="#aa0000"
android:layout_width="fill_parent"
android:layout_height="fill_parent"><FrameLayout
  android:id="@+id/content_frame"
    android:background="#00bb00"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerInParent="true" /></LinearLayout>
Wrapped answered 25/5, 2016 at 9:58 Comment(0)
I
1

Your Activity needs to be of the type MvxCachingFragmentCompatActivity or MvxCachingFragmentActivity to support hosting of fragments.

Secondly you need to ignore the normal inflator, and use binding inflate after that:

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    var ignore = base.OnCreateView(inflater, container, savedInstanceState);
    return this.BindingInflate(FragmentId, null);
}
Insurmountable answered 25/5, 2016 at 10:8 Comment(6)
Additionally be sure to override the FragmentPresenter in the setup.cs , if you dont already do that.Geri
Thank you like allways for ur fast answer. I thought the MvxActivity in the Fragging Package could support them two, because it has a List of Fragments and a OnAttachFragments Method. On top, I could not find the MvxCachingFragmentActivity or the xxxCompatActivity class :)Wrapped
Do I really need to use the support package even when I do not need to support older versions of android? I am just asking, because i can find the MvxCachingFragmentActivity only in the support package.Wrapped
There is a version of it in the FullFragging package too, but i would recommend using support because it also handles material design for Android 4.xInsurmountable
Thank you, then I will try it out.Wrapped
Would you be able to share a link where you found this in the documentation please?Flatten

© 2022 - 2024 — McMap. All rights reserved.