On Orientation change cannot update the fragment view in android - android

I have a MainActivity.java which has 2 fragments:
1) Fragment1.java -> Has a RecyclerView of items.
2) Fragment2.java -> Displays the details of the item when it is clicked.
In Portrait orientation only Fragment1.java or if item clicked then Fragment2.java is visible.
In Landscape mode both the fragments are visible side by side.
This works fine in both Portrait and Landscape orientations if I start the app in that orientation.
However if I change the orientation in between when the app is still running, then I get the error IllegalStateException : Cannot perform this action after onSaveInstanceState whenever another list item is clicked. The action on which it throws the error is transaction.commit().
Any idea how I can fix this?

I would suggest seeing if you have set the Orientation mode in the activity tag in the manifest and if you did remove it.Android handles this automatically.
such as :
<activity
android:name=".MyActivity"
android:screenOrientation="portrait" />
Regarding the different looks of the fragments when the app is in Landscape or Portrait , this has to do with the Layout you choose in the xml file.
Relative , Linear etc. Please checkout the Android official documentation and use that meets your needs.

Related

how to rotate a fragment inside an activity without activity rotation?

There is an android program
I have a bottom navigation bar inside my app that i don't want to change its orientation because of quality reduce
I want to rotate a fragment inside an activity when change orientation of the phone without activity rotation. how can i do this?
Thanks.
Open your Manifest.XML
Find your activity.
Add tag inside
<activity
android:configChanges="orientation"/>

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.

Android Activity height too long, how to swipe to bottom?

I have an Activity (single) and if I flip the device I can see only the half of the Activity. I have two EditText and a Button and I can't reach them because I can't swipe to bottom. If I flip back the device everything is ok.
Is there any method that I must call to can swipe?
Use ScrollView as Niek said.
2.Set Fixed Orientation if you Haven't designed for the LandScape mode,(i.e Either potrait or landscape)
You can set your fixed orientation in your Manifest file like this
android:screenOrientation="portrait" (You can apply this to your activity as well as for the application whole)
Use a ScrollView to wrap your layout.

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)

who is sending orientation changed event in android

I observed one problem in status bar that while we open it from the home screen and change the device to landscape mode, the orientation of the status bar is not changed to landscape,
but if we open any applications like messaging in which their orientation is changed according to device orientation and then open the status bar, it will also change the orientation.
That means keeping an application who orientation is constant in background and then opening the status bar will have this problem that orientation of status bar also becomes constant.
Please give me some suggestions so that I can rectify this problem. If I could know that who is parsing the xml file and if android:configChanges="keyboardHidden|orientation" is present in the manifest file who is deciding not to send Intent.ACTION_CONFIGURATION_CHANGED
you should prepaire to create landscape layout also.
for landscape mode-> layout-land
for portrait mode-> layout
android:configChanges="keyboardHidden|orientation" , this tag used when we dont want to change state in our activity when orientation is changed
if android:configChanges="keyboardHidden|orientation" is present in the manifest file then our activity will handle itself one or more configuration changes that we specify and need not to handle by android system.That means when we use android:configChanges="keyboardHidden|orientation" we tell android system that our activity itself controll this configuration change.

Categories

Resources