Reputation: 2443
Here's a small code in one of my activities.
Window window = this.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
window.addFlags(LayoutParams.FLAG_KEEP_SCREEN_ON);
I have already set the manifest to allow permission to use the WAKE_LOCK. Now, the thing I'm not sure about is the flags set in place in one activity.
Are they persistent throughout the entire app if one of many activities "add" a flag to the Window, like a screensaver flag?
My hunch is that if an activity were to set or add a flag, the flag is only active if the flagged activity is active. If it calls an Intent to start another activity (which didn't specify the flag), the set flag will then be canceled until the intented, unflagged activity finishes, which then it may revert back to its original set state in the flagged activity.
Am I wrong? Thanks in advance.
Upvotes: 3
Views: 6308
Reputation: 2294
Interesting question which is best settled with an experiment:
I just tested it by writing a little app with two activities, one opening the other by the click of a button. I have set the Screen timeout to 15 seconds.
Activity1 includes the commands to keep the screen on in its onCreate()
Method, Activity2 does not. Now while Activity1 is visible the screen does not turn off (obviously). 15 seconds after I open Activity2, it does. So you are right, the Flag is only valid as long as the activity is shown.
Interestingly it does not matter whether you call finish()
on Activity1. Even if Activity1 is not destroyed (onDestroy()
is not called, only onPause()
) the flag will be invalid.
If you return to the first Activity onCreate()
is actually not called again on Activity1 but th Flag is valid again (since the activity is resumed from the Stack with its previous features).
Upvotes: 7
Reputation: 63955
The Window
is always reset when an activity starts (onCreate
). You don't add flags to some global application window state.
You need to setup the Window
for each Activity
separately.
If it calls an Intent to start another activity..
.. the calling Activity
is destroyed and a new Activity
is created. The new one has a reset window and once you come back to the initial Activity
it has the window reset again. But you should get onCreate
called where you can setup the Window
again.
Upvotes: 7