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

1.5k Views Asked by At

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);
}
6

There are 6 best solutions below

0
Rujul Gandhi On BEST ANSWER

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.

0
Jitesh Mohite On

please refer below code

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

 getActionBar().setHomeButtonEnabled(true);
 getActionBar().setDisplayHomeAsUpEnabled(true);


 }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
 switch (item.getItemId()) {
 case android.R.id.home:

 // app icon in action bar clicked; goto parent activity.
 this.finish();
 return true;
 default:
 return super.onOptionsItemSelected(item);
 }

}

0
Josué de León Santana On

You need to call onBackPressed function

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

You need to override onBackPressed method to handle back button(home button) on ActionBar.

@Override
public void onBackPressed() {
   super.onBackPressed();
   // your code here
   // finish();
}
0
noobEinstien On

You have to set toolbar before get .

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);          
    setContentView(R.layout.activity_main);
    Toolbar mToolBar = (Toolbar) findViewById(R.id.app_bar);

    setSupportActionBar(mToolBar);
    getSupportActionBar().setTitle("Some titile);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

       mToolBar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            /**
             change code here*/
        }
    });
}
0
div On

For providing proper up navigation, you would have to:

Define the parent activity in the manifest.

<activity
   android:name=".MainActivity"
   android:parentActivityName=".HomeActivity"
   ....>

  <meta-data
    android:name="android.support.PARENT_ACTIVITY"
    android:value=".HomeActivity" />
</activity>

And in MainActivity:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

There should be a toolbar in the xml of MainActivity. And then override the onOptionsItemSelected.

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

Instead of using finish(), you should consider using NavUtils.navigateUpFromSameTask(this) or other such methods of NavUtils class.

Use launchModes like singleTop to prevent reloading the parent activity.

More info on proper up navigation can be found here.