Back arrow | Kotlin | Android

I have 2 activities. After switching from the first to the second, I want to display a back arrow in the AppBarLayout, which will return me to the first activity. How do I do this?

P. s: I need this because the back button toggles my menu, which is located in the activity itself.

Author: Sire IMPACTUS, 2020-07-17

1 answers

If you use ActionBarActivity , you can set this arrow as follows:

Toolbar toolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(toolbar);

Then call:

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);

You can also use this from the fragment:

((ActionBarActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
((ActionBarActivity) getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(true);

If you don't use ActionBarActivities or you want to set the Back arrow to Toolbar-e that doesn't use your SupportActionBar then you can do it like this:

actionBar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_action_back));

actionBar.setNavigationOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
       // Здесь вы можете обработать ваш клик
   }
});
 1
Author: Sergei Buvaka, 2020-07-18 17:35:00