面对 Jetpack Navigation 的奇怪问题,任何帮助将不胜感激。我有一个导航图,如下所示:
我想使用喷气背包导航从一个 fragment 导航到另外两个 fragment 。 到目前为止,我只能从 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;
}
}
};
单击第一个按钮时,我只会得到 toast ,单击第二个按钮时,我实际上可以导航到SecondFragment。我在这里做错了什么?以及使用导航进行导航的最佳实践是什么。
提前致谢
最佳答案
尝试使用NavHostFragment.findNavController(fragment).navigate(R.id.action_created_from_graph)
而不是 Navigation.createNavigateOnCLickListener
我记不清了,所以语法可能有点不对劲,但这就是它的要点。
关于java - Jetpack 导航上未执行导航操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60001296/