My app has navigation set up and it is a single activity app. Fragments are linked like this:
MainFragment
-> GameFragment
MainFragment
-> GameSetupFragment
-> GameFragment
What I need to accomplish: Depending on settings, GameFragment sometimes should be locked in the landscape mode. When I did this in an activity, I wrote a line of code:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
What can I do if I use fragments? Is there a way to tell the fragment owner (i.e. the activity) to switch the orientation?
CodePudding user response:
Assuming that you know that your fragment is attached to the activity, you can call requireActivity()
in the fragment to get your activity:
requireActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)