Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to override action bar back button in android?

Tags:

java

android

I want to customize the activity back button in action bar, not in hard key back button. I have overriden the onBackPressed() method. It works with my emulator back button, but not with action bar back button.

I want it to happen with action bar. How can I do this?

Here is my code:

@Override public void onBackPressed() {     Toast.makeText(getApplicationContext(),"Back button clicked", Toast.LENGTH_SHORT).show();      return; } 

I have used this toast whether back pressed is working or not but the actual implementation changes like to move back to previous activity. But this is not working with the button present on top of action bar (besides title of the activity).

Please any one could specify me the problem.

like image 978
G K Avatar asked Jan 21 '13 11:01

G K


People also ask

Can you disable the Back button in android?

Please navigate to Android->Advanced Restrictions->Display Settings. Uncheck the 'Hide navigation bar' option. This will disable the on-screen buttons: back, home and recent apps.

How do I get rid of the back arrow on my android toolbar?

getActionBar(). setDisplayShowHomeEnabled(false); //disable back button getActionBar(). setHomeButtonEnabled(false); In a older android phone, the back button is removed with these two code lines.


1 Answers

I think you want to override the click operation of home button. You can override this functionality like this in your activity.

@Override public boolean onOptionsItemSelected(MenuItem item) {     switch (item.getItemId()) {     case android.R.id.home:         Toast.makeText(getApplicationContext(),"Back button clicked", Toast.LENGTH_SHORT).show();          break;     }     return true; } 
like image 105
ibrahimyilmaz Avatar answered Sep 19 '22 15:09

ibrahimyilmaz