简体   繁体   中英

Nav action not executing on Jetpack Navigation

Facing a weird issue with Jetpack Navigation,any help would be much appreciated. I've a navigation graph as below:

I would like to navigate from one fragment to other two fragments, using jetpack navigation. So far I've been able to navigate only to the second_fragment from entry_fragment as shown in the picture below. 导航图

My 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;
        }
    }
};

On First Button Click I'm only getting the toast and on clicking the second button I can actually navigate to SecondFragment . What I'm i doing wrong here? and what is the best practice to navigate using Navigation.

Thanks in advance

try using NavHostFragment.findNavController(fragment).navigate(R.id.action_created_from_graph) instead of Navigation.createNavigateOnCLickListener

I'm going off my memory, so the syntax might be a little off, but that's the jist of it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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