简体   繁体   English

Android ViewModel 未从 BackStackEntry 中获取所选项目

[英]Android ViewModel not getting Selected Item from BackStackEntry

I have a map fragment that can be accessed from the bottom nav bar.我有一个 map 片段,可以从底部导航栏访问。 I also want to be able to access it from a click on my recyclerview (if it is clicked from the recycler view, display the location from the recyclerview).我还希望能够通过单击我的回收站视图来访问它(如果从回收站视图中单击它,则显示来自回收站视图的位置)。 To do this, I've attempted to check the backstack to see if the listfragment exists.为此,我尝试检查 backstack 以查看 listfragment 是否存在。 I can't get it to work though, can someone see the issue with my code?我无法让它工作,有人可以看到我的代码的问题吗?

ViewModel:视图模型:

    private final MutableLiveData<Fish> selected = new MutableLiveData<Fish>();

public void select(Fish item) {
    selected.setValue(item);
}

public LiveData<Fish> getSelected() {
    return selected;
}


public MapViewModel(Application application){
    super(application);
}

ListFragment(select is in onclick method): ListFragment(选择在 onclick 方法中):

mMapViewModel = ViewModelProviders.of(this).get(MapViewModel.class);
Log.d("view-click", String.valueOf(fish.getLocation()));

                mMapViewModel.select(fish);
                Navigation.findNavController(view).navigate(R.id.action_fishListFragment_to_mapFragment2);

MapFragment:地图片段:

NavController navController = NavHostFragment.findNavController(this);
    NavBackStackEntry backStackEntry = navController.getBackStackEntry(R.id.fishListFragment);

    mMapViewModel = new ViewModelProvider(backStackEntry).get(MapViewModel.class);
    mMapViewModel.getSelected().observe(backStackEntry, list -> {
        Log.d("MAPFRAGMENT",list.getLocation() + "");
    });

I get no errors, but I also don't get the MAPFRAGMENT log in the logcat window.我没有收到任何错误,但我也没有在 logcat window 中获得 MAPFRAGMENT 日志。 I've verified that my logcat is working, and that the onclick method is in fact setting the value in the viewmodel.我已经验证我的 logcat 正在工作,并且 onclick 方法实际上是在 viewmodel 中设置值。

ViewModelProviders.of(this) is associating the ViewModel with this - your Fragment itself, not the NavBackStackEntry (which is a whole separate layer above the Fragment layer). ViewModelProviders.of(this)将 ViewModel 与this相关联 - 您的 Fragment 本身,而不是NavBackStackEntry (它是 Fragment 层之上的一个完全独立的层)。

Instead, you'll want to use navController.getCurrentBackStackEntry() with new ViewModelProvider() in your FishListFragment to associate the ViewModel with the NavBackStackEntry that you're later accessing in the MapFragment:相反,您需要在 FishListFragment 中使用navController.getCurrentBackStackEntry()new ViewModelProvider()来将 ViewModel 与您稍后在NavBackStackEntry中访问的 NavBackStackEntry 相关联:

NavController navController = Navigation.findNavController(view);
NavBackStackEntry currentBackStackEntry = navController.getCurrentBackStackEntry();
mMapViewModel = new ViewModelProvider(currentBackStackEntry).get(MapViewModel.class);
Log.d("view-click", String.valueOf(fish.getLocation()));

mMapViewModel.select(fish);
navController.navigate(R.id.action_fishListFragment_to_mapFragment2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM