Manage toolbar's navigation and back button from fragment in android

前端 未结 8 2050
庸人自扰
庸人自扰 2020-12-02 10:00

All of my fragments are controlled through ActionBarActivity (mainActivity), inside mainActivity a DrawerLayout is implemented and all the child fr

8条回答
  •  半阙折子戏
    2020-12-02 10:28

    The easiest solution I found was to simply put that in your fragment :

    androidx.appcompat.widget.Toolbar toolbar = getActivity().findViewById(R.id.toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NavController navController = Navigation.findNavController(getActivity(), 
    R.id.nav_host_fragment);
                navController.navigate(R.id.action_position_to_destination);
            }
        });
    

    Personnaly I wanted to go to another page but of course you can replace the 2 lines in the onClick method by the action you want to perform.

提交回复
热议问题