sam
sam

Reputation: 113

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

Upvotes: 0

Views: 1551

Answers (6)

div
div

Reputation: 1533

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.

Upvotes: 0

noobEinstien
noobEinstien

Reputation: 3293

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*/
        }
    });
}

Upvotes: 0

Ajith Pandian
Ajith Pandian

Reputation: 1352

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

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

Upvotes: 0

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

Upvotes: 0

Jitesh Mohite
Jitesh Mohite

Reputation: 34270

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

}

Upvotes: 0

Rujul Gandhi
Rujul Gandhi

Reputation: 1390

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.

Upvotes: 4

Related Questions