java - 导航操作未在 Jetpack Navigation 上执行
问题描述
面对 Jetpack Navigation 的一个奇怪问题,任何帮助将不胜感激。我有一个导航图如下:
我想使用喷气背包导航从一个片段导航到其他两个片段。到目前为止,我只能从entry_fragment导航到second_fragment,如下图所示。
我的 EntryFragment.class :
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_entry,container,false);
v.findViewById(R.id.fragmentOneButton).setOnClickListener(ls);
v.findViewById(R.id.fragmentTwoButton).setOnClickListener(ls);
return v;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
private final View.OnClickListener ls = new View.OnClickListener() {
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.fragmentOneButton:
Toast.makeText(getActivity().getApplicationContext(), "First Button", Toast.LENGTH_SHORT).show();
Navigation.createNavigateOnClickListener(R.id.action_entry_fragment_to_first_fragment).onClick(view);
break;
case R.id.fragmentTwoButton:
Toast.makeText(getActivity().getApplicationContext(), "Second Button", Toast.LENGTH_SHORT).show();
Navigation.createNavigateOnClickListener(R.id.action_entry_fragment_to_second_fragment).onClick(view);
break;
}
}
};
在第一个按钮单击时,我只得到吐司,在单击第二个按钮时,我实际上可以导航到SecondFragment。我在这里做错了什么?以及使用导航进行导航的最佳做法是什么。
提前致谢
解决方案
尝试使用NavHostFragment.findNavController(fragment).navigate(R.id.action_created_from_graph)
而不是 Navigation.createNavigateOnCLickListener
我要忘记我的记忆,所以语法可能有点不对劲,但这就是它的要点。
推荐阅读
- angular - 如果使用 Angular 以反应形式选中复选框,则显示 div
- csv - 将 csv 导入 Nextjs 并使用 d3 读取
- google-apps-script - 从谷歌表格单元格获取文本到谷歌文档
- .net-core - 如何在 .net api 网关中使用 gRPC 服务?
- android - 当我改变位置时,我的 IP 在 Visual Studio 代码中看起来不同
- sql - 错误地将多个表添加到 masterdb
- typescript - 使用 mapbox 作为 VGeosearch 的提供者
- c# - 游戏结束后如何重新开始游戏?
- python - 访问函数中的变量而不在 Python 中调用它
- flutter - Flutter int counter...添加范围