What does this 4 line java code means in android application?
Asked Answered
H

3

5

In my java application i have this code

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

when i build it for android 2.3 (level 10) it compiles and works fine. But when i build it for android 4.0 (level 15) it compiles and gives me crash at run time and following error

07-16 14:00:03.090: E/AndroidRuntime(29487): FATAL EXCEPTION: main
07-16 14:00:03.090: E/AndroidRuntime(29487): java.lang.IllegalArgumentException: Window type can not be changed after the window is added.

when i comment this line and build it works fine and no issue..

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

so i am not getting why this happning and whats this code means?

Edit : some reference are here

when I run app on my phone.The version is android 4.0.3

toddler safe app on android

Hobbs answered 24/9, 2012 at 6:54 Comment(2)
It simply means when your activity comes to foreground like onDisplay in blackberryGrapery
Simply set targetSDK to less then 14 . Then it will work https://mcmap.net/q/83497/-window-type-errorOddball
H
9
@Override
public void onAttachedToWindow()
  {
  this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
  super.onAttachedToWindow();
  }

is used to disable home button in android but

this security flaw has been fixed in newer versions of Android so it will not work in ICS and jelly bean...!!

Hobbs answered 15/10, 2012 at 7:7 Comment(1)
It's giving me the error that java.lang.IllegalArgumentException: Window type cannot be changed after the window is added.Viceroy
R
4

I've solved this issue putting

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

in onCreate before calling super.

 protected void onCreate(Bundle savedInstanceState) {

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

        super.onCreate(savedInstanceState);
}

Saludos desde Medellín

Rigi answered 14/11, 2013 at 18:26 Comment(0)
M
2
@Override    
public void onWindowFocusChanged(boolean hasFocus) {
    this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);    
    super.onWindowFocusChanged(hasFocus);
}

I had some problems with windowAttached as well, try using windowFocusChanged instead.

Moth answered 24/9, 2012 at 7:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.