Jai Saxena
Jai Saxena

Reputation: 255

My homeAsUp button is not working in android

I have created a navigation drawer activity, from the main activity i launch 2 different activities. In both of these activities i have used actionBar.setDisplayHomeAsUpEnabled(true); to enable homeasup button. how do i find the ids of these home buttons so that i can work with them in onOptionsItemSelected? Here's my method:

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            Toast.makeText(this, "pressed1", Toast.LENGTH_SHORT).show();
            return true;
        }
        else if (id == R.id.home){
            Toast.makeText(this,"pressed2",Toast.LENGTH_SHORT).show();
            finish();
        }
        else{
            Toast.makeText(this,R.id.homeAsUp + " " + id + " " + item,Toast.LENGTH_SHORT).show();
        }

        return super.onOptionsItemSelected(item);
    }

R.id.home doesnt seem to work. The toast gives a correct output for all other actions but for home button the item string in toast shows: com.android.internal.view.menu.ActionMenuItem@b8d9a09.

Upvotes: 2

Views: 1134

Answers (1)

tyczj
tyczj

Reputation: 73753

its android.R.id.home not R.id.home

Upvotes: 6

Related Questions