1 Activity, 2 Fragments and setting orientation - android

I have an Activity where the 1st Fragment (whose orientation is locked) solicits input from a user. This data is used to determine how to orient the 2nd Fragment. This orientation will also be locked.
I am guessing I will have to make a call to setRequestedOrientation( ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) either from the Activity or the 2nd Fragment.
My question is where best to do this. Does it really make a difference? Can this action be performed w/o undergoing an Activity restart?

If changing the setRequestedOrientation state requires the screen to rotate then it will restart the activity.
For example if you request ActivityInfo.SCREEN_ORIENTATION_PORTRAIT while the screen is in landscape, it will restart.
However it will not restart if the phone is already in that orientation.

Related

How to make a one fragment screen always in landscape mode and reamaining not.?

I have one activity which i am using as host for calling all my fragments.I am using setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE) for setting screen in landscape mode.But problem is that it is making every fragment in landscape mode because their is only one parent activity for all of them.I can not use android:screenOrientation="landscape" because this will also work the same.
I want one of my fragment to be displayed always in landscape mode and remaining fragment as according to device orientation.Is their is another ways for doing it?
If you wanna show one specific Fragment in landscape you can call
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
in the onCreate() of your Fragment.
In this Fragments onPause() f.e. you can call
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
which is the default value and enables screen rotation changes.

How to lock screen orientation when anctivity isn't recreated

I have an activity used to display a video (VideoView).
By default the user can change screen orientation to see the video.
I avoid destroying/recreating the activity upon orientation changes because of latency issue.
The rotation is painless without any sound break.
android:configChanges="keyboardHidden|orientation|screenSize"
What I want to do now is add a button that will force the activity to remain on current orientation.
The problem is that because the activity isn't recreated after the rotation, I don't know which method to override to avoid the rotation to happen.
Overriding onConfigurationChanged is useless because the rotation already happened.
Use setRequestedOrientation().
E.g. to lock the orientation to portrait, call:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
In your activity.

Android: Fragments causing problems on orientation change

According to the Google example here I developed an app based on fragments.
My main activity contains a listfragment of titles and, if it is created in landscape mode, a details fragment. If the app is startet in portrait mode, the main activity contains only the listfragment and, if a list item is clicked, start a new activity which shows the detailsfragment.
If I stay in either the portrait or landscape mode, everything works fine. But as soon as I change the orientation multiple problems occur.
1st problem: starting in portrait mode, then changing to landscape mode, the activity is added to the activity stack twice and I have to press the back button twice to close my app. I cant image this is the way Google wants this to work, so how do I avoid this?
2nd problem: when changing from landscape mode to portait mode, the list is shown and not the detailsfragment with the currently selected item. Therefore, all the user input in my detailsfragment is lost. This is just annoying and I don't know how to handle this. Do I have to care about the orientation change programmatically in every activity?
3rd problem: When I switch between n details in landscape mode, as soon as I change to portrait mode, I have to press the back button n times to close my add as the fragments are in the back stack (although they aren't visible any more). Do I have to clean the back stack myself in orientation change?
There is one thing about Activities. That is, when you change the orientation, the Activity restarts unless you do the following:
-First, add this in your manifest (inside the activity tag), so you will be able to tell the application what to do in case you change the orientation:
android:configChanges="orientation"
-Second, implement the following method in case you need to to something in case the orientation changes. If not, with the one before the user won't loose its data.
onOrientationChanged (int orientation)

onConfigurationChanged not getting called the first time

I've a problem trying to capture the onConfigurationChanged event. This is the scenario:
Activity A starts (listens to onConfigurationChanged)
Phone rotated to landscape mode (onConfigurationChanged being called). Start activity B.
Activity B starts (listens to onConfigurationChanged) (LANDSCAPE)
Activity B rotates back to portrait (onBackPressed event raised). Activity B is destroyed and A is called back.
Activity A resumes
Phone rotated to landscape mode. The onConfigurationChanged is not called this time.
Phone rotated to portrait mode. The onConfigurationChanged called.
Phone rotated to landscape mode. The onConfigurationChanged called.
Why step 6 don't call onConfigurationChanged event? it doesn't make sense at all. Do you know what could be the issue?
I have the same problem, I update UI onResume() to fix it.
I had exactly the same issue. I still don't understand this behavior of android system, but you can use onOrientationChanged of OrientationEventListener instead of configuration change handling.
See this answer (example is not perfect but shows the way): https://stackoverflow.com/a/13844242/554281
I have faced same problem and was stuck in this for more than a week. Then I have prepared a sample APP with tabHost and reproduced the same issue on the sample app. After playing around with that, I found that this is a bug of android's tabHost. So, I have migrated tabHost to FragmentTabHost and found the problem is gone.
The simulation of the Issue with tabHost:
Let, there are two tab i.e A and B
Simulation 1 (Arise the bug)
Arrive on A in portrait mode
Go to B on portrait mode
Rotate B to landscape
Back to A on landscape mode.
Rotate A to portrait (onConfigChanged method of tabActivity gets fired. But onConfigChanged of A activity doesn't get fired)
Simulation 2 (Works fine)
Arrive on A in portrait Mode
Go to B on portrait mode (continue rotating as you want, but stop on portrait mode)
Back to A on portrait mode.
Rotate A to landscape (both onConfigurationChanged method of tabActivity and A activity gets fired)
Summary: If you get back to the screen in the same orientation you got out, both onConfigurationChanged() will get called.

Is there a way I can prevent an app from going to the home screen when orientation is changed?

I snap a photo, and it gets placed onto an ImageView. When the user sees the page where the Image View is, and I turn the phone sideways where the orientation is horizontal, the app immediately returns to the app's main screen. Any ideas?
Rotating the phone recreates the activity. Implementing the onSaveInstanceState() and onCreate() methods to properly save and restore the state of the activity should fix the problem.

Categories

Resources