Another developer use case: If the WindowManager
or getWindow()
is being called on onCreate()
or onStart()
or onResume()
, a BadTokenException
is thrown. You will need to wait until the view is prepared and attached.
Moving the code to onAttachedToWindow()
solves it. It may not be a permanent solution, but as much as I could test, it always worked.
In my case, there was a need to increase the screen brightness when the activity became visible. The line getWindow().getAttributes().screenBrightness
in the onResume()
resulted in an exception. Moving the code to onAttachedToWindow()
worked.