I want to use a separate layout for my fragment on landscape view , hence I have created a separate layout for landscape view . But the problem is when I rotate the screen onConfigurationChanged , onDestroyView onDestroy called sequentially . And it backs to previous fragment . I can prevent it by using android:configChanges="orientation|screenSize" on my activity . But in this case , the view remains same it doesn't use my separate layout , it is just stretched to fill the screen . Is there any way so that I can use separate layout while using android:configChanges="orientation|screenSize" ?
To use different layouts use different resourse folders:
That is, use layout folder for portrait and layout-lang for landscape mode.
you can use getResources().getConfiguration().orientation to detect orientation, then load XML you like something like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
switch (getResources().getConfiguration().orientation) {
case Configuration.ORIENTATION_PORTRAIT:
setContentView(R.layout.aportrait);
break;
case Configuration.ORIENTATION_LANDSCAPE:
setContentView(R.layout.alandscape);
break;
}
/////..............
}
your issue is quite generic,
If you disable the orientation and make it a constant to portrait/landscape, the changes in orientation of the device (phone) will not force the activity to be re-created. so even though you have separate layout for fragment it will not be loaded,
and if you make orientation of activity depending on the orientation change to the device (Phone) it will re-create the activity due to which you activity will initialize everything as result you are getting the first fragment while rotating the screen.
to deal with it, what you can do is use savedInstanceState(Bundle) while recreating the activity,
save the current fragment you have loaded and mention that to your savedInstanceState(Bundle) and after re-creating the activity when the orientation is changed, read the savedInstanceState and you will know which fragment to load
Please remove andorid:configChanges from manifest. Its purpose is that you yourself is handling configuration changes. Remove it and android will automatically pick the right layout file.
Hope this helps.
Related
I want two different layouts (one for portrait and one for landscape), but when I change the orientation, I don't want to repeat onCreate.
Is it possible to do so?
Normally when changing the orientation onDestroy() and right after that onCreate() of the Activity is called.
However you can avoid this: Handling Configuration Changes Yourself
EDIT: For a different layout for portrait or landscape mode you can use two layouts in onCreate() and pick one based on the orientation (I know you wanted to avoid the onCreate method, but I don't think it is possible otherwise). Just check the current orientation and with this.getResources().getConfiguration().orientation act accordingly.
Maybe you can avoid using onCreate() and do the same thing in onConfig but I am not sure about that.
I am new to android development . I have been puzzled by this problem in recent times .
Every time orientation changes my view gets reloaded. I want to avoid this. This is happening on change from landscape to portrait and vice versa.
Put below line in your manifest in activity:
android:configChanges="orientation|screenSize"
So, your activity should look like:
<activity
android:configChanges="orientation|screenSize"
android:name=".YourActivity"/>
This way prevent destroying activity on orientation changes.
The reason why Android is reloading the view after the orientation has changed is that this view is most probably another view than the one that was displayed before the orientation change happened, e.g. with new layout, font sizes, imageviews or other resources. The Android system is able to automatically load suitable resources for different screen dimensions (e.g. height, width, density ... ) in case of an orientation change.
For this to happen, Android first calls the activity's onDestroy() method, followed by a call to its onCreate() method to enable the activity to reload resources and activity state.
If for some reason you do not want this behaviour, you can follow the above advice and add an android:configChanges attribute to your activity element. In this case Android calls the activity's onConfigurationChanged() method instead of onDestroy() and onCreate(). onConfigurationChanged() is the method to place your code to adapt to the new orientation, if necessary. If not, it can also be left empty.
For further reading, see http://developer.android.com/guide/topics/resources/runtime-changes.html
I have an activity with a videoview and a button, I have two different layout, one for portrait mode and second for landscape.
So, I create layout-land for second layout, and all works fine.
The problem is when rotation occours. Videoview restart becouse Activity renew.
Setting in manifest orientation|screensize video not restart, but I lose landscape layout.
How to do both tasks ?
Keep the configChanges in your AndroidManifest.xml, but also make use of onSaveInstanceState and onRestoreInstanceState to put the activity back in to the state you want it to be in after the orientation has changed.
Look here for more details
I have created two folders layout and layout-land with two xml files, one for portrait and the other for landscape. Both of the xmls work but here is the problem.
My first screen is a login screen and my second screen is a main screen. If I login in portrait and then turn my phone landscape at the main screen. The layout will landscape turn but it uses the portrait xml for the main screen.
The same error occurs if I start in landscape and try to move to portrait later on.
It seems like whatever layout I do for my main then that's the layout that will be used for the rest of the app. Is there anyway to go around this?
Also. I'm already using android:configChanges="orientation" in my manifest for the activities.
If you are using android:configChanges="orientation", then you can override onConfigurationChanged to inflate the new layout after a configuration change.
#Override
protected void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(...);
}
Make sure you have a good reason for preventing the Activity from being recreated on an orientation change... and most importantly don't just do it because orientation changes are crashing your app. Handling the configuration change yourself can make it much more difficult to use alternative resources, because the system does not automatically apply them for you. This technique should be considered a last resort when you must avoid restarts due to a configuration change and is not recommended for most applications.
Using android:configChanges="orientation" means you will be handling the orientation change in code. If you want it to automatically switch layouts, you shouldn't have that there.
Make sure both xml files present in two different folders have same name.
I build my Android Application and want do add layouts for different orientations now. I created a layout-land folder and put a different layout for my first Starter Activity "myStartActivity" (with the same name as the layout i used before for both orientations) in there.
Depending on my Screen Orientation BEFORE i start the app the right layout is chosen: "myLayout.xml" from within the "layout"-folder when i start in portrait and the "myLayout.xml" from within the "layout-land"-folder when i start in landscape.
The Problem is, that when i rotate the device when I'm already in the Activity, after rotation I dont get the new layout. For example: rotating from portrait to landscape it stills shows "myLayout.xml" from within the "layout"-folder and not the "layout-land"-folder as it should.
I didnt overwrite any OnConfigurationChange Methods or anything. All I do in "myStartActivity" is instantiate some buttons and give them some listeners. I wanna use a different layout in landscape to change the ordering of the buttons.
In my case the described problem happens only when I have android:configChanges="orientation"
in the activity in the manifest.
Otherwise the correct layout is automatically used when rotating.
What you could do is use Activity.getResources().getConfiguration().orientation
Then depending on the orientation result, set the content view in your oncreate() method which is called on rotation.
protected void onCreate(Bundle savedInstanceState) {
int result = this.getResources().getConfiguration().orientation;
if (result == 1){
//set content view to portrait
setContentView(R.layout.portrait);
}
else{
//set content view to landscape}
setContentView(R.layout.landscape);
}
Or stick in a case statement :)
If you're testing on the emulator only, there may be problems with detecting orientation change. I have experienced that at least.