22

I've tried the advice here, the advice here, the advice here, I've commented out the onAttachedToWindow() in my Base Activity. I have two Activities inheriting from this class, BaseActivity. One runs, and one does not. What could be the difference? My target SDK is 19; changing it to 12 makes no difference. Here is my onCreate for BaseActivity:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onAttachedToWindow();
    super.onCreate(savedInstanceState);

    ....
    }

When navigating to the second activity, stepping through the code, it makes it through onCreate(), onResume(), then crashes.

What could be the problem?

Stacktrace:

06-26 13:41:57.963  28667-28667/com.assistek.ediary E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.assistek.ediary, PID: 28667
java.lang.IllegalArgumentException: Window type can not be changed after the window is added.
        at android.os.Parcel.readException(Parcel.java:1550)
        at android.os.Parcel.readException(Parcel.java:1499)
        at android.view.IWindowSession$Stub$Proxy.relayout(IWindowSession.java:903)
        at android.view.ViewRootImpl.relayoutWindow(ViewRootImpl.java:5301)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1507)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
        at android.view.Choreographer.doCallbacks(Choreographer.java:580)
        at android.view.Choreographer.doFrame(Choreographer.java:550)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5254)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

EDIT: if I change to target API 12 and put all of the changes in from onCreate into onAttachedToWindow, I can get this exception to go away, but I'd like the target SDK to be 19.

My new onCreate():

@Override
protected void onCreate(Bundle savedInstanceState) {

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
...
}

My new onAttachedToWindow():

@Override
public void onAttachedToWindow() {
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
    super.onAttachedToWindow();
}

This only works with target API 12.

Answer Min target must be less than 14 when WindowManager.LayoutParams.TYPE_KEYGUARD used

Community
  • 1
  • 1
Kristy Welsh
  • 7,828
  • 12
  • 64
  • 106
  • try this for API 19 : https://developer.android.com/training/system-ui/immersive.html – Fouad Wahabi Jun 30 '15 at 20:36
  • 3
    Do not call `super.onAttachedToWindow()` anywhere other than from inside your override of `onAttachedToWindow()`, if you have one. While I cannot speak to `TYPE_KEYGUARD`, the rest of that code should be after `super.onCreate()`, but before any `setContentView()` call. Beyond that, see if you have anything interesting in your manifest that might explain the difference between your two activities. – CommonsWare Jul 01 '15 at 16:21
  • The only thing that's different between the two activities is: android:clearTaskOnLaunch="true" – Kristy Welsh Jul 01 '15 at 16:27
  • I did also move everything back to onAttachedToWindow. – Kristy Welsh Jul 01 '15 at 16:30
  • someone mentioned allready taht the super.onCretae() should be called at the beginning of onCreate(), so try this. And also you dont need to call the onAttachedWindow() method! – Ilja KO Jul 06 '15 at 06:33

5 Answers5

7

Try to use this for window :

requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                     WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_adjectives);
Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
parik dhakan
  • 787
  • 4
  • 19
2

I run into the same question. But after stopping adding

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
requestWindowFeature(Window.FEATURE_NO_TITLE);

before super.onCreate(savedInstanceState); of BaseActivity, it works well. I cut and paste above codes before super.onCreate(savedInstanceState); of descends of BaseActivity.

BTW, I don't think you have to call super.onAttachedToWindow(); inside methods of life circle of Activity. Because onAttachedToWindow(); is called when the view is attached to a window when you overriding a View.

SilentKnight
  • 13,761
  • 19
  • 49
  • 78
2

I also had this problem, but I solved it by removing the Window, changing the params and then adding the window again. This was good enough for me.

WindowManager.removeView(View); params = params2; //changed the params to something else WindowManager.updateViewLayout(View, params); WindowManager.addView(View, params);

Anonymous
  • 21
  • 1
  • This was the only thing that worked for me in an AccessibilityService overlay. But the `updateViewLayout` isn't necessary since the view has already been removed from the window and will be re-added in the next line. – Brad Feb 14 '22 at 15:59
1

Remove

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

From your onAttachedToWindow(), like this:

 @Override
     public void onAttachedToWindow() {
        //this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG|WindowManager.LayoutParams.FLAG_FULLSCREEN);

          super.onAttachedToWindow();
     }
TofferJ
  • 4,678
  • 1
  • 37
  • 49
0

Sorry for late reply. You should add:

requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);

in onCreate method if you are using api level 15 or above. It works for me.

Nikolay Mihaylov
  • 3,868
  • 8
  • 27
  • 32