简体   繁体   中英

How to change orientation to portrait in Fragment Class - Android

My application is in Landscape mode .. I'm trying to change the Fragment in Portrait view ..

Used the below code to rotate from landscape to portrait ..

getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

But the Fragment "onCreate" and "onCreateView" called twice ..

Can anyone please advice on implement Portrait View in Fragment class ? Or advice to how to avoid calling the onCreateView twice ?

In the Manifest file your activity should adds,

"android:configChanges="keyboardHidden|orientation|screenSize"

For example,

<activity
    android:name=".MainActivity"
    android:configChanges="keyboardHidden|orientation|screenSize"
    android:label="@string/app_name"
    android:screenOrientation="landscape" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
</activity>

Specifically you should add "screenSize" in the Android manifest file. So that your Fragment "onCreate" and "onCreateView" won't be called twice.

Override setRetainInstance. This should solve your problem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM