I use the Navigation Drawer, and in a Fragment that is open I want to have the active screen.
I use:
getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
But this thing light up the screen in every fragment of the navigation drawer. How can I get the screen active only in one fragment?
You just create new Fragment class which extend Fragment and their respective XML file and call them in MainActivity eg. Show activity on this post. In android studio 3.5 and above, this comes by default. You will notice different fragment are generated by default.
When each fragment is selected from the navigation drawer, for those you want to have the screen kept on do:
getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
for those you what to have normal screen dimming and turn-off do:
getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Another method for controlling the screen that is more convenient in some cases is View.setKeepScreenOn(). It can be called on any view. It will keep the screen on as long as the view is visible.
You can also control screen-on with the android:keepScreenOn attribute in the layout XML.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With