How to change orientation to portrait in Fragment Class - Android - 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

Related

Preventing an App from changing to Portrait Orientation when the Device is unlocked

I'm developing a game that runs in a wrapped webview for Android devices. Everything is working as expected--in this case, I have locked my view to landscape orientation, and prevented the device from restarting the activity if the phone is rotated.
However, if I put my device to sleep and then unlock it, I see my application in portrait mode for a moment, until my device re-orients to landscape. This behavior makes sense, since a phone is naturally used in portrait orientation, but I don't want my activity to switch from landscape.
If I start any other game, by which I mean those presumable written in native code, that is locked into landscape mode, and then lock and unlock my phone, the game doesn't do the same quickly-switch-from-portrait-to-landscape dance.
What am I missing?
In MainActivity I have
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE)
in several places, including onConfigurationChanged, onWindowFocusChanged, onPause, and onResume.
Here is how my activity is defined in my AndroidManifest
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:hardwareAccelerated="true"
android:screenOrientation="sensorLandscape"
android:configChanges="orientation|screenSize"
android:launchMode="singleTask"
android:alwaysRetainTaskState="true"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
android:windowSoftInputMode="stateAlwaysHidden" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Thanks for any help/advise!
-J.L.
You forgot to remove sensor from the screenOrientation tag. Try this:
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:hardwareAccelerated="true"
android:screenOrientation="landscape"
android:configChanges="orientation|screenSize"
android:launchMode="singleTask"
android:alwaysRetainTaskState="true"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
android:windowSoftInputMode="stateAlwaysHidden" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
the app still shows as the incorrect orientation for a split second when the phone is unlocked.
Did you move setContentView(R.layout.main) to the onResume, AFTER where you put your orientation code? I would try setting the orientation, then the setContentView()

Activity orientation restore

There is an Activity named PlayActivity which is used to play videos, and I set this activity to use the landscape model by xml:
<activity
android:name=".activity.PlayActivity"
android:screenOrientation="landscape">
Now once I enter the activity from another Activity say it is MainActivity, the PlayActivity will use the landscape model as expected, however once I hit the back button to return to MainActivity, I found that the MainActivity is changed to landscape too, how to avoid this?
Try to put your MainActiviy activity in the portrait, since you can configure the whole application in one direction to customize each one of them to leave an example of how serious greetings.
<activity
android:name=".activity.MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".activity.PlayActivity""
android:screenOrientation="landscape"
/>

How to set application fragments to portrait mode only?

I have an Android app which has a main activity and 3 Fragments which are tabs. I would like the application to remain in portrait mode at all times but I can't seem to get this working. This is what I have tried, as per another stack overflow post, but I'm not sure what I'm doing wrong....does it need to be different if using fragments?
<activity
android:name="com.tutorial.test.activities.act1"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Thank you!!
Edit: The ViewPager is on the FragmentActivity for which I am setting the screenOrientation as above.
Try this..
You can try with programmatically.
After rootView in your java add this line getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
For Ex:
View rootView = inflater.inflate(R.layout.activityxml, container, false);
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
And also in your manifest change it android:configChanges="orientation|keyboardHidden" as android:configChanges="keyboardHidden"
<activity
android:name="com.tutorial.test.activities.act1"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden" >
Orientation attribute is per activity so you can declare the orientation for only the activity that contains the fragment so that it is in landscape and the rest of the activities will remain as they are.
getActivity().setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
or you can declare in the manifest
<activity android:name=".Control" android:screenOrientation="portrait"></activity>

How to keep android rotation landscape always

I want to keep landscape mode always.
I Lock screen to portrait mode on Nexus7, I set android:screenOrientation="sensorLandscape" and android:configChanges="orientation|keyboardHidden"in AndroidManifest.xml, when I go to other activity and back to this activity, then the screen will rotate portrait and rotate to landscape again (Didn't call onCreate method again), but I don't want to rotate the screen in any situations, how to fix it?
I think this is the answere you're looking for:
stackoverflow.com/a/2730894/2249774
For All your Activities in the manifest set
android:screenOrientation="sensorLandscape" android:configChanges="orientation|keyboardHidden"
Example :-
<activity android:name="com.example.test.testActivity" android:label="#string/app_name" android:screenOrientation="sensorLandscape" android:configChanges="orientation|keyboardHidden"">
<intent-filter>
<action android:name="com.example.test.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Repeat this for all activities used in the manifest..
Try editing your AndroidManifest.xml to look like this
<activity
android:name=".YOURACTIVITYNAME"
android:screenOrientation="landscape" />
In your AndroidManifest file, try typing in the following code:
android:configChanges="keyboard|orientation|keyboardHidden|screenSize"
android:screenOrientation="landscape"
example:
<activity
android:name="com.gaspar.slinfilipino.Quiz"
android:label="#string/title_activity_quiz"
android:configChanges="keyboard|orientation|keyboardHidden|screenSize"
android:screenOrientation="landscape"
android:parentActivityName="com.gaspar.slinfilipino.SignLanguageMenu" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.gaspar.slinfilipino.SignLanguageMenu" />
</activity>

Android activity Restarts When orientation changes

My app seems to restart when it detects an orientation change. Well not really.
I only have portrait orientation enabled though.
In log cat I can see that my app is still doing calculations, but my progress dialog disappears.
What is going on?
In my manifest file this is my activity.
<activity
android:name=".xxxActivity"
android:label="#string/app_name"
android:configChanges="orientation"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Is there a specific reason why you're listening for the orientation change with android:configChanges="orientation"?
If not, remove that and I suspect your problem will go away.
add this right after the super call
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Better to change configChanges to
android:configChanges="orientation|keyboardHidden"
I guess, that activity is restarted, because you forgot to add onConfigurationChanged(Configuration) method in your activity.
http://developer.android.com/reference/android/app/Activity.html#onConfigurationChanged(android.content.res.Configuration)
It can be empty but shold be in the activity.
Also android:screenOrientation="portrait" is not needed.

Categories

Resources