向自定义工具栏添加后退按钮的最佳方法是什么?

所以我知道如何向我的自定义工具栏添加向后导航按钮,但是,必须有比将这些代码复制并粘贴到每个活动中更有效的方法来向多个活动添加功能...

        Toolbar toolbar = findViewById(R.id.tlbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

I've tried making a common class were i create a function that could be called in each activity however you need to findViewById which can only be done in the activity. Any suggestions around this?