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
Fab Animation with Viewpager/Tabslider
On Android, How to Switch Activities Programmatically
Set a Consistent Style to All Edittext (For E.G.)
No Cached Version Available for Offline Mode
How to Make an Android View That Flips Between Views on Swipe/Fling
Listfragment Does Not Accept My Layout
Redirect Users to Itunes App Store or Google Play Store
How to Be Notified When a Snackbar Has Dismissed Itself
To Run Dex in Process, The Gradle Daemon Needs a Larger Heap. It Currently Has Approximately 910 Mb
Android Active Link of Url in Textview
Calling Camera from an Activity, Capturing an Image and Uploading to a Server
Constraintlayout Chains and Text Ellipsis + Image on The Right
Access Ordered Images and Video in Same Cursor
Zipalign - Command Not Found - MAC Terminal
How to Hide The Soft-Key Bar on Android Phone
Httpurlconnection.Openconnection Fails Second Time
How to Specify Location of Debug Keystore for Android Ant Debug Builds