I have a FragmentActivity with some fragments in them. When I go from the fragment I am on (Fragment A) which I have the phone in the portrait position to the next fragment (Fragment B) which I have set in the code setOrientation() to Landscape I get a black screen when I hold the phone in portrait and the layout will not show on the second fragment until I turn my phone to Landscape. I was under the impression that no matter how you are holding the phone that when you do setRequestedOrientation() it should forcefully show the layout you provided
Visual:
----------- -----------
| | | | ---------------
| | | | | |
| Frag A | -> | Frag B | -> Until I turn -> | Frag B |
| |(then to)| (black) | it to Landscape | (not black |
| | | | | anymore) |
| | | | ---------------
----------- -----------
Code:
MainActivity which holds the orientation changes:
// Set Orientation of the Activity.
public void setOrientation(int sensor) {
if (sensor == 1) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
} else if (sensor == 2) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
} else if (sensor == 3) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
} else if (sensor == 4) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} else if (sensor == 5) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
}
FragA:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Set the orientation to be in portrait and landscape.
((MainActivity) getActivity()).setOrientation(1);
...
}
FragB:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Set the orientation to be in landscape.
((MainActivity) getActivity()).setOrientation(5);
...
}
You must call getActivity() in onActivityCreated!
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