0

I've been trying to disable the home button using the following code:

 @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_HOME) {
        Log.i("TAG", "Press Home");
        System.exit(0);
        return true;
    } else {
        return super.onKeyDown(keyCode, event);
    }
}

@Override
public void onAttachedToWindow() {    
  this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
  super.onAttachedToWindow();}      

}

But this crashes the app.

The crash is caused specifically by the line:

this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);

Why does this happen? What can be done to solve this issue?

The crash Log:

06-25 17:47:18.620 18125-18125/com.example.myapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                      Process: com.example.nandagp.qapp, PID: 18125
                                                                      java.lang.IllegalArgumentException: Window type can not be changed after the window is added.
                                                                          at android.os.Parcel.readException(Parcel.java:1962)
                                                                          at android.os.Parcel.readException(Parcel.java:1904)
                                                                          at android.view.IWindowSession$Stub$Proxy.relayout(IWindowSession.java:966)
                                                                          at android.view.ViewRootImpl.relayoutWindow(ViewRootImpl.java:7029)
                                                                          at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2264)
                                                                          at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1738)
                                                                          at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7745)
                                                                          at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
                                                                          at android.view.Choreographer.doCallbacks(Choreographer.java:723)
                                                                          at android.view.Choreographer.doFrame(Choreographer.java:658)
                                                                          at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
                                                                          at android.os.Handler.handleCallback(Handler.java:789)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:98)
                                                                          at android.os.Looper.loop(Looper.java:164)
                                                                          at android.app.ActivityThread.main(ActivityThread.java:6938)
                                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                                          at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Nand gopal
  • 346
  • 3
  • 17

2 Answers2

0

You should remove this code

this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);

From the onAttachedToWindow() method and put it into the onCreate one.

Hope this helps

PS: for the home button override, read this post. Disabling it is not easy and not reccomended, it will result in user unable to exit your application and (imo) it is not a good idea unless you are running in kiosk mode. So think accurately if you really need to do it and, if you do, refer to the first link I posted

Pier Giorgio Misley
  • 5,305
  • 4
  • 27
  • 66
  • It stops the app crash but doesn't disable the home button. – Nand gopal Jun 25 '18 at 12:41
  • @Nandgopal added a couple of rows – Pier Giorgio Misley Jun 25 '18 at 12:45
  • That post gives a similar answer as well. – Nand gopal Jun 25 '18 at 12:49
  • @Nandgopal why should you override the home button? – Pier Giorgio Misley Jun 25 '18 at 12:49
  • I need to prevent the user from closing the app or moving to another, from that activity. I have been able to disable the back button and the recent apps button. Is there a way to remove the actionbar instead? – Nand gopal Jun 25 '18 at 12:52
  • you can do it by editing the theme in your style file and using noactionbar one – Pier Giorgio Misley Jun 25 '18 at 12:53
  • I tried that. It is causing the app to crash as well. – Nand gopal Jun 25 '18 at 12:54
  • check again on the web, there are many answers.. or also post a new question and close/solve this – Pier Giorgio Misley Jun 25 '18 at 12:54
  • I would prefer an answer to this question, as finding a way to disable the home button would be my choice, as i need the other 2 buttons to perform other actions. Removing the actionbar is just my last resort. – Nand gopal Jun 25 '18 at 12:59
  • refer to the other link for this, simply you can't just do it but you have to manage the entire home state.. You can't create a normal app that overrides the home button, this would have no sense since the user would not be allowed to go back to the home. It's like if you invite someone at your home and allow him to change the door lock whenever he wants, would you do this? – Pier Giorgio Misley Jun 25 '18 at 13:04
  • Only one specific activity needs the home button disabled. This activity has other buttons that allow the user to go to other activities where home button is enabled. – Nand gopal Jun 25 '18 at 13:06
  • yes, but it's like saying "if you are in my kitchen, you can change the door lock". As you can understand, it is better to always say no than to say "sometimes yes". Expecially because if you wrongly manage this, the user will be locked in forever.. not a good idea to allow this – Pier Giorgio Misley Jun 25 '18 at 13:07
  • This app is not for general users or for public use. It is for a very specific set of users. All i need is a solution to this or to understand why the crash happens. – Nand gopal Jun 25 '18 at 13:09
  • google doesn't care at all of your final use, they just don't want this to appen. Then if you sell your app to anyone or if you only use that when you are in your bathroom, for them is the exact same stuff.. If you want to do this, create a launcher app or a kiosk mode – Pier Giorgio Misley Jun 25 '18 at 13:12
0

When user pressed "KEYCODE_HOME" you "shutdown" application:

if (keyCode == KeyEvent.KEYCODE_HOME) {
        Log.i("TAG", "Press Home");
        System.exit(0);

You will be delete System.exit(0)