Nested fragment problems on orientation change - android

I have this situation.
A drawer layout -> each section is a fragment
A section contain a page viewer (3 fragments )
Each fragment contains a recyclerview and each item is a fragment
I have some problems.
When the orientation of the screen changes i move always to the first fragment of the page adapter.
If i write something in one of these fragments and the sceeen orientation change lose everything.
Same problems with onPause etc.
How can I manage this situation?
thanks.

Make sure the you are using getChildFragmentManager() instead of getFragmentManager()/getSupportFragmentMAnager() inside a nested Fragment.

Maybe it's will be helpful. Just paste it in your AndroidManifest file
android:configChanges="orientation|keyboardHidden|screenSize"

Add this android:configChanges="keyboardHidden|orientation|screenSize">
inside activity of your AndroidManifest.xml
and
adding this to every fragment is working fine for me.
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}else{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}

Related

Android : Fragment onCreateView method call again after orientation changed

I implement the app which has two layout for landscape and portrait mode. The layout for landscape is in the layout-land. I have the fragment1 for portrait layout and fragment2 for landscape layouts. I override the onCreateView in each fragment.
I have static variable to know the current fragment. I assgined in the onCreateView (1 for fragment1 and 2 for fragment2).
My problem is that the static value is still 1 when the orientation is landscape mode.
I debugged the orientation of application. When I change orientation portrait into landscape, fragment2's onCreateView method called first and then the fragment1's onCreateView method called again. The static value has overridden.
I don't know why did fragment1 onCreateView method call after the fragment2 called? I want to assign the right value for right fragment.
Sorry for my bad English.
Thanks.
You don`t need to save something!
Just let your activity handle the orientation change.
In AndroidManifest.xml put this
<activity
android:name=".MainActivity"
android:configChanges="orientation|screenSize" >
</activity>
You need to save the bundle and must override onsavedinstance method so that the activityis not createdagain.
First when orientation is changed android checks for the savedinstancestate and calls onSavedInstanceState method if implemented.
Inside the onCreate of each fragment you have to call
getActivity().setRequestedOrientation(requestedOrientation)
You should have two special Fragment implementations, if your Fragments have a different 'business logic' in landscape and portrait. If they just have a different layout then use 1 Fragment implementation, and create 2 layouts, one for each orientation.
Instantiate and create your Fragments in Activity.onCreate(). But do not save the current Fragment in a static variable. Instead ask the FragmentManager if a Fragment has already been added:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Fragment myFragment = getSupportFragmentManager().findFragmentByTag("myTag");
if(myFragment == null){
//no Fragment has been added
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(new MyFragment1(), "myTag");
transaction.commit();
}else{
//the fragment has been added already, possibley before orientation change
//you could check its type and replace it
if(fragment instanceof MyFragment1){
}else if(fragment instanceof MyFragment2{
}
}
}

Tab Widget Issue when use android:configChanges="orientation|keyboardHidden" in Grid View but working for other Tab

I am stuck with my issue.Thing is that its a custom Tab Widget.In that have multiple
tab like Home - News - Abc - PQR .
The Activity should be for both orientation like portrait and landscape. so for that each tab have two xml for portrait which is store at layout-port/file.xml and landscape which store at layout-land/file.xml
For manage orientation portrait to landscape i have added android:configChanges="orientation|keyboardHidden" rule tag in each activity.
TAB_SAMPLE.java Tab file.
TAB_GROUP_ACTIVITY each Tab Group activity File
file.java Task file
After all this stuff i get issue here :
If i addandroid:configChanges="orientation|keyboardHidden"rule tag in tab_sample activity then its working perfect. like manage different view. port to land and land to port but its not working in Home.java.
Now if i remove android:configChanges="orientation|keyboardHidden" rule tag in tab_sample activity then its working for Home activity not for News.java
Mean when i change the orientation its keeping same xml form port not use from layout-land.in the sense its call OnCreate() again.
So as i found may be issue is in Tab Widget.
Update
Now after tracing my code i get that main issue is in grid view activity because its only activity which is not working.
Issue is between Tab host v/s Grid View. I don't know why its not taking layout-land xml file. i found this as same issue but no replay on that question also
see in Detail manifestfile.xml
I want to maintain both portrait and landscape in all activity.
Both XML File
Please help me how to solve this.
Oooohhh Finally i got the solution for above issue. It's was very difficult.
For maintain the state of orientation Landscape to portrait and vice-versa we generally adding android:configChanges="keyboardHidden|orientation" property tag under activity.
But here may be issue is Tab_Group_ Activity due to that i am not able to maintain state in GridView. Grid_File.java is Only single java file which was not handling the orientation rest of all other working perfectly.
Now if i remove android:configChanges="keyboardHidden|orientation" from TAB_SAMPLE.java then Its handling only Grid_File.java not others.
mean that was keeping same Layout XML in landscape also where i have two separate XML File.
Here is my solution:
I have add android:configChanges="keyboardHidden|orientation" in TAB_SAMPLE.java as well as
implement onConfigurationChanged(Configuration newConfig) and set Number of column of grid. like gridView.setNumColumns(6);
#Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
// gridView.setSelection(index);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
{
// Log.e("On Config Change", "LANDSCAPE");
gridView.setNumColumns(6);
} else
{
// Log.e("On Config Change", "PORTRAIT");
gridView.setNumColumns(4);
}
}
Generally we are adding either android:configChanges="keyboardHidden|orientation" tag under activity or implementing onConfigurationChanged(Configuration newConfig) but here i have written both.

Show different layout for both orientations

In my android application i designed two different layouts with same file name say my_profile.xml, and stored in two different directories i.e, 1) res/layout, 2) res/layout-land. Now the problem is if i start activity in Portrait mode it loads Portrait mode layout but after changing orientation it doesn't change layout, But if i start activity in Landscape mode it loads layout of landscape i.e, perfect. Problem is only when i change orientation, it doesn't handle it automatically. Can any one tell me, what can be the problem?
Check for the following :
1) In manifest file check for the below line in your activity
android:configChanges="orientation|keyboardHidden|screenSize"
2) Override the below function
#Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
Log.d(tag,"onconfig");
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
// do something
Log.d(tag,"land");
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
// do something
}
}
-Preeya
In your manifest.xml make the changes like this,,,,,
<activity android:name=".myActivity" android:windowSoftInputMode="adjustPan" android:configChanges="keyboardHidden"></activity>
dont use configuartionChanges="orientation"
Thats it...
Check your manifest file. If you have the following in your activity then REMOVE IT:
android:configChanges="keyboard|orientation|screenSize"
orientation causes the the activity to resort to the same layout rather than creating a new one.

Duplication of listview rows in list fragment on orientation change

I have a ListFragment where I use a custom adapter to populate the listview. All is well until I change orientation and scroll. Then it looks like this:
I am guessing it has something to do with me fumbling with a view holder, but I can't access the code right now.
The reason for the overlapping fragment was that I used FrameLayout and added the fragment with FragmentTransition.add(...). When I changed .add() to .replace() the old fragment was removed and the new one was added and my problem was solved.
I had similar problem and according to that i am telling the solution :-
you are getting this blur because every time on orientation change somewhere new instance of listfragment is created (may be it is in oncreate()), so you have to just make an instance of list fragment once and on orientation change replace that fragment rather than adding again.
Changing orientation causes onCreate to restart unless you include this method
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
}
and put this in the activity section of your manifest
android:configChanges="orientation"
Check that this is the first time onCreate() is called,
in other words, determine if the callback is not due to screen rotation.
protected void onCreate(Bundle savedInstanceState) {
if(savedInstanceState == null) {
// transition.add(Your fragment,...)
}
}
I had the same problem, and its because of FrameLayout usage,
First you have to check if your fragment has already added to the activity :
String TagName = "F";
Adding a fragment without a UI If you want to get the fragment from the activity later, you need to use findFragmentByTag(). "Google"
http://developer.android.com/guide/components/fragments.html#Adding
Fragement F = getFragmentManager().findFragmentByTag(TagName);
Then check
if(F == null) {
F = new F();
getFragmentManager()
.beginTransaction()
.add(R.id.container, F, TagName)
.commit();
}
the target here is to avoid adding or creating new instance of the fragment, that persist during the configuration change which causes the problem when using the FrameLayout as container.
Solution 2 (Simple):
The only thing you have to do here is to change your container to ex:LinearLayout , and that's it. but in my opinion this is not the best solution because of multiple instances of the fragment.
Hope this help;

how to Manage Child activity within Tab Acivity when orientation changes?

I'm using tab widget.
When my child activity is running and I change the orientation,child activity destroy.
As a solution of this problem,I added
android:configChanges="orientation|keyboardHidden in all activity tags in my manifest.xml file.
I found that my app doesn't take xml file from layout_land folder.
Can anyone give me solution for this query?
Thanks in advance.
I found that using onConfigurationChanged method i can know the orientation and i have to set landscape file in layout folder instead of layout_land folder.
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){
setContentView(R.layout.login_landscape);
}
else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
setContentView(R.layout.login);
}
}
This happens because your Activity is not destroyed since you have put android:configChanges="orientation|keyboardHidden" in manifest so the setContentView is not called when orientation changes and therefore it doesn't pick the layout from the layout_land
Update : And this is not a problem with your Tab-Activity try it in normal activity it will not pick the correct layout or in general speaking no layout is picked your portrait layout is only rotated to be displayed in landscape

Categories

Resources