Custom “navigate up” behavior for certain fragment using Navigation component

前端 未结 4 391
心在旅途
心在旅途 2021-02-04 01:58

I want to add custom up navigation from fragment using Navigation component

In my build.gradle(app) I use androidx.appcompat:appcompat:1.1.0-

4条回答
  •  一生所求
    2021-02-04 02:43

    Add click event to toolbar back button in this way

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
     switch (item.getItemId()) {
    case android.R.id.home:
        // Toolbar back button pressed, do something you want    
    
    default:
        return super.onOptionsItemSelected(item);
      }
    }
    

    Another way

      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        // Title and subtitle
        toolbar.setNavigationOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // Toolbar back button pressed, do something you want    
            }
        });
    

提交回复
热议问题