0

I want to disable the home button of my app. I achieve to disable the back button by the following code.

@Override
public void onBackPressed() {
    // Do nothing
    return;
}

Now I want to disable the home button as well.I tried by using various way as mentioned on Internet like,

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

But it was showing the following exception

java.lang.IllegalArgumentException: Window type can not be changed after the window is added.
                                                       at android.os.Parcel.readException(Parcel.java:1687)
                                                       at android.os.Parcel.readException(Parcel.java:1636)
                                                       at android.view.IWindowSession$Stub$Proxy.relayout(IWindowSession.java:953)
                                                       at android.view.ViewRootImpl.relayoutWindow(ViewRootImpl.java:5695)
                                                       at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1758)
                                                       at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1258)
                                                       at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6348)
                                                       at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
                                                       at android.view.Choreographer.doCallbacks(Choreographer.java:683)
                                                       at android.view.Choreographer.doFrame(Choreographer.java:619)
                                                       at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
                                                       at android.os.Handler.handleCallback(Handler.java:751)
                                                       at android.os.Handler.dispatchMessage(Handler.java:95)
                                                       at android.os.Looper.loop(Looper.java:154)
                                                       at android.app.ActivityThread.main(ActivityThread.java:6123)
                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)

So after searching I moved to the another solution like,

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

but still I am able to interact with the home button.

As well as I tried by using this way.

//disable the home button
    @Override
    protected void onPause() {
        super.onPause();
        Intent intent = new Intent(LoginActivity.this,LoginActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        startActivity(intent);
    }

This is also the good solution ,but not applicable for here. My target device is starting from API19 and above. Please help if anyone have the solution.

  • Possible duplicate of [How to disable the home key](https://stackoverflow.com/questions/3898876/how-to-disable-the-home-key) – Ratilal Chopda Nov 09 '17 at 06:37
  • I tried this, but app is crashing by throwing an exception java.lang.IllegalArgumentException: Window type can not be changed after the window is added. –  Nov 09 '17 at 06:42
  • Check this link: https://stackoverflow.com/questions/3898876/how-to-disable-the-home-key This might help. – Numanqmr Nov 09 '17 at 06:45
  • check out this approach (drawing on top of the screen) https://stackoverflow.com/questions/17549478/how-to-disable-home-and-other-system-buttons-in-android/20945431#20945431 – Dmitrii Z. Nov 09 '17 at 06:46

0 Answers0