How to close activity and go back to previous activity in android

后端 未结 18 1831
孤独总比滥情好
孤独总比滥情好 2020-12-07 08:19

I have a main activity, that when I click on a button, starts a new activity, i used the following code to do so:

Intent intent = new Intent(this, SettingsAc         


        
18条回答
  •  春和景丽
    2020-12-07 08:54

    on onCreate method of your activity write the following code.

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

    Then override the onOptionsItem selected method of your activity as follows

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                finish();
        }
        return super.onOptionsItemSelected(item);
    }
    

    And you are good to go.

提交回复
热议问题