Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting a black screen when using setRequestedOrientation()

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);
    ...
}
like image 894
JoeyL Avatar asked May 10 '26 09:05

JoeyL


1 Answers

You must call getActivity() in onActivityCreated!

like image 118
zella Avatar answered May 11 '26 23:05

zella