Action Bar Back Button Not Working

Action bar Back button not working

I solved these problem by adding the below coding in GalleryActivity.

ActionBar actionBar;
actionBar=getActionBar();

actionBar.setDisplayHomeAsUpEnabled(true);

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

return super.onOptionsItemSelected(item);
}

In MainActivity:

Previously,

public class HomeActivity extends BaseActivity

Then I change into

public class HomeActivity extends FragmentActivity

In GalleryFragment:

I use Intent to pass it to the GalleryActivity.

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Gallery gallery = (Gallery) arg0.getAdapter().getItem(arg2);

Intent intent = new Intent(getActivity(), GalleryActivity.class);
intent.putExtra("position", position);
intent.putExtra("id", gallery.getGalId());
intent.putExtra("name", gallery.getAlbumTitle());
startActivity(intent);

// mCallback.OnGalItemSelected(gallery.getGalId(),gallery.getAlbumTitle());
}

Display back button of action bar is not going back in Android

Add the following to your activity.You have to handle the click event of the back button.

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

Back button of Toolbar doesn't work

Add

if(item.getItemId() ==android.R.id.home){
onBackPressed();
}

to your onOptionsItemSelected override.

This way, when you're tapping that button you'll invoke the onBackPressed method, which is the default action to go back (a cleaner solution than brutally invoking finish())

That should do it.

why the back button doens't work by default

Clicking the up button triggers the onSupportNavigateUp and as mentioned in the official docs,

If a parent was specified in the manifest for this activity or an activity-alias to it, default Up navigation will be handled automatically.

To implement the expected behaviour override onSupportNavigateUp instead of onOptionsItemSelected

    override fun onSupportNavigateUp(): Boolean {
return navController.navigateUp()
}

Edit:

The Up and Back buttons are used to navigate up the hierarchy and the difference between them is

  • Back, the system button(left facing triangle), is used to navigate up the hierarchy and when in the home screen/fragment in your app and on pressing back, you will be navigated out of the app.

  • Up, the left-facing arrow in your appbar, is used to navigate up the hierarchy but it doesn't take you away from the app.

check out the docs for more info

Action Bar Back Button is not showing in Android

add the property

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

to show the "back button"

ActionBar's back home button is not working in fragment

in Activity...

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
//do something here like

int backStackEntryCount
=getSupportFragmentManager().getBackStackEntryCount();

if (backStackEntryCount > 0) {

getSupportFragmentManager().popBackStack();

}

return true;
}
return false;
}

in onCreate of Fragment put...

 this.setHasOptionsMenu(true); //setHasOptionMenu(true) was not working

and onOptionsItemSelected callback inside fragment is like...

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//do something with your id
return super.onOptionsItemSelected(item);
}

Action bar back button not working in webview

Try this Use android.R.id.home

@Override
public boolean onOptionsItemSelected(MenuItem item) {

int id = item.getItemId();

if (id == android.R.id.home) {
Toast.makeText(this, "Back Button clicked", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
this.finish();
}
return super.onOptionsItemSelected(item);
}

Back button is Not working in actionBar

You need to instruct the android stack that your fragment will handle the options menu instead of the holder activity by calling setHasOptionsMenu(true) and it's recommended by the docs to put it in your fragment's onCreate() callback. This will make your log message to be called appropriately.



Related Topics



Leave a reply



Submit