Proper way to handle action bar up button?

后端 未结 6 1915
没有蜡笔的小新
没有蜡笔的小新 2020-12-02 20:50

I use ActionBarSherlock (although I don\'t think it matters).

I have a Main activity and an About activity. I want the About activity to show the back-arrow by its l

6条回答
  •  攒了一身酷
    2020-12-02 21:11

    In your onCreate(Bundle savedInstanceState), do

    ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);
    

    Then in your onOptionsItemSelected(MenuItem item), do

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                // go to previous screen when app icon in action bar is clicked
                Intent intent = new Intent(this, PreviousActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
    

提交回复
热议问题