user434
user434

Reputation: 165

Back action button on toolbar of menu item in drawer layout not working

I've a drawer layout in my main activity. On selecting one of its menu item i.e Social (in my case), it jumps to another activity containing fragments tab layout. The toolbar in my Social activity has a back button <- like this. I want this to work and return to main content activity but i don't know :

Upvotes: 1

Views: 89

Answers (2)

Intsab Haider
Intsab Haider

Reputation: 3561

First step:Add this code in onCreat

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

Step:2 override onOptionItemSelected

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

        return super.onOptionsItemSelected(item);
    }

Upvotes: 2

V-rund Puro-hit
V-rund Puro-hit

Reputation: 5534

Try this way

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ...

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

    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    ....
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home) {
        // do your work
    }
    return super.onOptionsItemSelected(item);
}

Upvotes: 0

Related Questions