MvvmCross / MvvmCross-AndroidSupport

Android support library packages for MvvmCross: The .NET MVVM framework for cross-platform solutions.
http://mvvmcross.com
15 stars 0 forks source link

MvxCachingFragmentCompatActivity with a Non cached fragment #286

Open jeremy-alley opened 8 years ago

jeremy-alley commented 8 years ago

I'm trying to create a Non-cacheable fragment by setting the IsCacheableFragment attribute on my fragment. However, when I do this, the ViewModel never gets set. Using your example project I did the following.

Steps to reproduce

Set the IsCacheableFragment equal to false

[MvxFragment(typeof(MainViewModel), Resource.Id.content_frame, true, IsCacheableFragment =false)]
[Register("example.droid.fragments.ExampleRecyclerViewFragment")]
public class ExampleRecyclerViewFragment : BaseFragment<ExampleRecyclerViewModel>
{...

I get a null exception error in OnCreate of the Fragment due to the ViewModel being null

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
        var view = base.OnCreateView(inflater, container, savedInstanceState);

        var recyclerView = view.FindViewById<MvxRecyclerView>(Resource.Id.my_recycler_view);
        if (recyclerView != null)
        {
            recyclerView.HasFixedSize = true;
            var layoutManager = new LinearLayoutManager(Activity);
            recyclerView.SetLayoutManager(layoutManager);
        }
         //ViewModel is null
        _itemSelectedToken = ViewModel.WeakSubscribe(() => ViewModel.SelectedItem,
            (sender, args) => {
                if (ViewModel.SelectedItem != null)
                    Toast.MakeText(Activity,
                        $"Selected: {ViewModel.SelectedItem.Title}",
                        ToastLength.Short).Show();
            });
         ....

Expected behavior

I thought this would allow the fragment to not be cached. I was able to achieve my desired results by doing something similar to this. http://stackoverflow.com/questions/37195393/mvxcachingfragmentcompatactivity-refresh-cached-fragment

I posted this question to stackoverflow as well. http://stackoverflow.com/questions/39111689/mvxcachingfragmentcompatactivity-with-a-non-cached-fragment

Actual behavior

The ViewModel is null in the OnCreate method of my fragment

Configuration

Version: 4.2.3