I want to use setRequestedOrientation( ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
inside a class that extends Fragmnet
, where it should be best used? inside onCreateView()
, onActivityCreated
or onCreate()
? and how? because when I tried to use it, eclipse underline it with red squiggle.
Add a fragment to an activity You can add your fragment to the activity's view hierarchy either by defining the fragment in your activity's layout file or by defining a fragment container in your activity's layout file and then programmatically adding the fragment from within your activity.
We can use Android Studio support in the Design view for the MainActivity layout file to select a fragment from inside the Custom choices. Open the activity_main layout file in design view and, inside the Palete, click Fragment under the “Custom” section. You will be prompted to select a fragment.
To programmatically add or remove a Fragment, you will need the FragmentManager and FragmentTransaction instances. Simply stated and per the Android documentation, a FragmentManager manages the fragments in an Activity. Obtain the FragmentManager by calling getFragmentManager( ) in an Activity.
I think you should precede
setRequestedOrientation( ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
with getActivity()
so you can get instance of the current activity your Fragment
is associated with.
And, I think you could use it inside onCreateView()
You can use it inside of onCreateView() by calling your activity like thi
Activity a = getActivity();
if(a != null) a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
A note here is that it changes your activites orientation when this fragment is called. You cannot change the orientation of a fragment, but you can change the orientation of the activity from a fragment.
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