How to display and set click event on Back Arrow on Toolbar?

前端 未结 7 1050
-上瘾入骨i
-上瘾入骨i 2020-12-02 18:31

How can I set back arrow in Android toolbar and also apply click listener?

相关标签:
7条回答
  • 2020-12-02 19:06

    Complete example here http://www.freakyjolly.com/how-to-add-back-arrow-in-android-activity/

    Use getSupportActionBar() Activity on which you want to show Back Icon

    In OtherActivity.class

    public class OtherActivity extends AppCompatActivity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.other_activity);
    
    
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    }
    
    public boolean onOptionsItemSelected(MenuItem item){
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
    
    public boolean onCreateOptionsMenu(Menu menu) {
        return true;
    }
    

    }

    This will add a event listen

    0 讨论(0)
提交回复
热议问题