How to catch navigation icon click on toolbar from fragment?

add code block toolbar.setNavigationOnClickListener after setSupportActionBar(toolbar)


This works for me.

toolbar.setNavigationOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Toast.makeText(getApplicationContext(),"your icon was clicked",Toast.LENGTH_SHORT).show();
    }
});

 toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            switch (item.getItemId()) {
                case android.R.id.home:

                  // do what ever you want here
            }
            return true;
        }
    });