Search code examples
androidandroid-actionbarandroid-actionbaractivity

Activity is not returning to previous activity while clicking on ActionBar back navigation button


My Activity is extending ActionBarActivity and we are setting a back navigation button in onCreate() :

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);          
    setContentView(R.layout.activity_main);

    if(getSupportActionBar()!=null)
    {
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
}

and for back press, finishing this activity is on given overridden method but it's not moving to previous activity

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

Solution

  • Your id is wrong. so please change your id 'R.id.home' to 'android.R.id.home:'.

    switch (item.getItemId())
    {
        case android.R.id.home:
            finish();
            return true;
     }
    

    Try this.