繁体   English   中英

导航操作未在 Jetpack Navigation 上执行

[英]Nav action not executing on Jetpack Navigation

面对 Jetpack Navigation 的奇怪问题,任何帮助将不胜感激。 我有一个导航图如下:

我想使用喷气背包导航从一个片段导航到其他两个片段。 到目前为止,我已经能够仅定位到从entry_fragmentsecond_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 我在这里做错了什么? 以及使用 Navigation 进行导航的最佳实践是什么。

提前致谢

尝试使用NavHostFragment.findNavController(fragment).navigate(R.id.action_created_from_graph)而不是 Navigation.createNavigateOnCLickListener

我记不清了,所以语法可能有点不对,但这就是它的要点。

暂无
暂无

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

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