MvxCachingFragmentActivity example

Update - Thanks to @ Martijn00 and @ Plac3Hold3r, I was able to update my application to use MvxCachingFragmentCompatActivity, but it just does not work correctly. I find that if I return again, the ViewModel will be available, but the commands in the view model will be empty.

Also, if I go back and then go over, some of the buttons will not respond. I guess this is the same problem.

What I really need to know is what gives me additional functionality, and how to use it correctly.

The original question follows ...

I run into a problem when the view model for a fragment is zero. I suspect that my activity clears when I open the camera activity. For all my applications, I use one action, and all my views are fragments.

When the cameraโ€™s action is completed, activity is restored, but one of the fragment view models is zero. I am currently using AppCompatActivity for my only action, but in the course of my research, I probably should use MvxCachingFragmentActivity. The problem is that I have no idea how I should use it. I canโ€™t find anything.

Does anyone have a working example of how to use MvxCachingFragmentActivity or MvxCachingFragmentCompatActivity.

I cannot find anywhere where he tells me how I should use it.

I found this other link, but I think it is out of date and the other link given in this example is 404.

If anyone knows a simple selection and whether this will work with a single action, please let me know.

thanks

0
source share
1 answer

Customization

Mainactivity

Create an action that inherits from MvxCachingFragmentCompatActivity or MvxCachingFragmentActivity .

 [Activity] public class MainActivity : BaseFragmentActivity<MainContainerViewModel> { } 

MainContainerViewModel

Create a Viewmodel to bind to an Activity. You will never navigate directly using this Viewmodel. Instead, you will navigate to this Viewmodel using fragments that set MainContainerViewModel as the parent model of the Viewmodel.

 public class MainContainerViewModel : MvxViewModel { } 

XML Layout Example

Add a layout to your MainActivity . Remember to include FrameLayout with id. In this case, content_frame . This identifier is important because you define the frame where to place your fragment. You specify multiple FrameLayout's if you want more than one fragment for the same view.

 <?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/coordinator_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.design.widget.AppBarLayout android:id="@+id/appbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary"> <TextView android:id="@+id/textview_toolbar_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@android:color/white" android:layout_gravity="left" style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title" /> </android.support.v7.widget.Toolbar> </android.support.design.widget.AppBarLayout> <FrameLayout android:id="@+id/content_frame" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> </android.support.design.widget.CoordinatorLayout> 

Homefragment

In terms of fragments, you need to include the MvxFragment attribute, which needs the Viewmodel type associated with the activity in which you want to place your fragment. In addition, you need to specify the identifier for FrameLayout , which is found in the action layout where you want to place the fragment.

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

Using

When navigating, you can now use ShowViewModel<HomeViewModel>() , which will move around the home fragment. But, importantly, you must first run the required MainActivity before navigating the fragment. This allows you to improve overall navigation with other platforms that do not require these Viewmodels containers, i.e. Automatically processed through agreement.


Notes

You can specify multiple MvxFragment attributes. This is useful if you want the same fragment to be separated by several actions. The Top MvxFragment will be used by default. If you are currently in the context of any other MvxFragment attribute MvxFragment , then navigation will occur.

If your Setup.cs is not inherited from MvxAppCompatSetup or you are using a custom presenter, you need to make sure that you also register the presenter with IMvxAndroidViewPresenter . This is important as MvxCachingFragmentCompatActivity or MvxCachingFragmentActivity allow IMvxAndroidViewPresenter to go to the desired fragment.

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

You can also check out Sample Repo for an example of this.

+3
source

Source: https://habr.com/ru/post/1015337/


All Articles