How to go back to previous fragment from activity?

前端 未结 8 1408
太阳男子
太阳男子 2021-02-07 08:26

I\'ve got an app with nav drawer, which is switching fragments. From inside one of those fragments, I am calling a new activity. When I click back in this activity (in toolbar),

8条回答
  •  庸人自扰
    2021-02-07 08:49

    I had the same problem and got fixed. The only thing you need to do is to override the method "onOptionsItemSelected" in the activity:

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    

    android.R.id.home is your first fragment in the menu.

提交回复
热议问题