MvxCachingFragmentCompatActivity refresh cached fr

2019-07-26 01:22发布

问题:

I have a project based off the XPlatformMenus MvvmCross sample. The app is using one activity inherited from MvxCachingFragmentCompatActivity. The app uses a NavigationView sliding drawer to allow navigation to different fragments that are swapped out at runtime.

I have one fragment which displays a list using RecyclerView. I can scroll down to the bottom of the list, hit the back button, then invoke the fragment again from the navigation menu, and the fragment is displayed exactly as it was (with the scrolled position at the bottom of the list). How can I make it so the fragment always starts as if it was created for the first time after I call ShowViewModel<TViewModel>? I think I don't want this specific fragment to be cached, or I want to have some kind of event I can hook into to reset the ViewModel when the fragment is being displayed again. How can this problem be solved?

回答1:

I have not tested this yet, but looking at MvxCachingFragmentCompatActivity there seems to be virtual method you can override

protected override void ShowFragment(
    string tag, 
    int contentId, 
    Bundle bundle, 
    bool forceAddToBackStack = false, 
    bool forceReplaceFragment = false)

The forceReplaceFragment parameter is commented as:

If you want the fragment to be re-created.

Maybe something like:

protected override void ShowFragment(
    string tag, 
    int contentId, 
    Bundle bundle, 
    bool forceAddToBackStack = false, 
    bool forceReplaceFragment = false)
{
    if (tag.Equals(typeof(<<VIEW_MODEL_TYPE>>).FullName))
        forceReplaceFragment = true;

    base.ShowFragment(tag, contentId, bundle, forceAddToBackStack, forceReplaceFragment);
}