Overriding the home and back button is the case when the boolean value is true

I was wondering if I can override the action of the back and home buttons in some cases. Usually these buttons should just react as they always have, but in the case when some settings are correct, I want to redefine the buttons and allow them to call my own methods.

I use these two methods to override these buttons:

@Override public void onBackPressed() { // call my backbutton pressed method when boolean==true } @Override public void onAttachedToWindow() { this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD); super.onAttachedToWindow(); // call my homebutton pressed method when boolean==true } 
+10
android override button back-button
Apr 05 2018-12-12T00:
source share
6 answers

I was wondering if I can override the action of the back and home buttons in some cases.

Yes, you can override the Home button.

I developed an application that disables the hard button, you can see. I took the toggle button, which blocks all hard buttons for work except Power

 public class DisableHardButton extends Activity { /** Called when the activity is first created. */ TextView mTextView; ToggleButton mToggleButton; boolean isLock=false; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mTextView=(TextView) findViewById(R.id.tvInfo); mToggleButton=(ToggleButton) findViewById(R.id.btnLock); mToggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub isLock=isChecked; onAttachedToWindow(); } }); } @Override public boolean dispatchKeyEvent(KeyEvent event) { if ( (event.getKeyCode() == KeyEvent.KEYCODE_HOME) && isLock) { mTextView.setText("KEYCODE_HOME"); return true; } else return super.dispatchKeyEvent(event); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // TODO Auto-generated method stub if( (keyCode==KeyEvent.KEYCODE_BACK) && isLock) { mTextView.setText("KEYCODE_BACK"); return true; } else return super.onKeyDown(keyCode, event); } @Override public void onAttachedToWindow() { System.out.println("Onactivity attached :"+isLock); if(isLock) { this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD); super.onAttachedToWindow(); } else { this.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION); super.onAttachedToWindow(); } } } 

main.xml

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/tvInfo" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <ToggleButton android:id="@+id/btnLock" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textOff="UnLocked" android:textOn="Locked" /> </LinearLayout> 
+41
Apr 05 '12 at 9:18
source share

You call super.onBackPressed () to call the regular method. Example:

 @Override public void onBackPressed() { if (activated) { //doyourthing } else { super.onBackPressed() } } 
+4
Apr 05 2018-12-12T00:
source share

No, you can’t. What you can do is ovveride the method and control the boolean inside it:

eg:

  public void onBackPressed() { // call my backbutton pressed method when boolean==true if (myCondition) { // take care of my needs } else // call super to let the back behavior be "normal" } 
+3
Apr 05 2018-12-12T00:
source share

Regarding the redefinition of the behavior of the Home button, you're out of luck .

However, if your application is specific and has a limited target audience, for example, an interaction application, a hospital kiosk, a restaurant order, you can try to make your application as a Home application (launch application). Here you can find a good example: How to write a custom Launcher application in Android

And to override the events of the back key, there are many examples.

For example:

  • Catch keypress with android
  • Cancel the back button to act like a home button
  • Android - How to override "Back" so that it does not complete () my activity?
+2
Apr 05 2018-12-12T00:
source share

enter a member variable of a variable of type boolean

 boolean temp; @Override public void onBackPressed() { // call my backbutton pressed method when boolean==true if(temp) //your methode else finish(); } 
+1
Apr 05 2018-12-12T00:
source share

I use this:

 public void onBackPressed() { switch (screen) { case 1: screen = 99; setContentView(R.layout.menu); break; case 99: finish(); break; } return; } 

When I am on a different screen (another menu screen), I set the variable screen to 1. When I press the back button, it returns to the menu screen (instead of killing the application). and give the screen variable number 99, and then when you press the back button again, it will kill the application.

However, you cannot change the home button.

+1
Apr 05 2018-12-12T00:
source share



All Articles