My problem is that I try to be able to invert the screen or the layout from portrait, because I define it as portrait and then I want to change it to landscape, without it updating or reloading the activity.
My code:
I use this code setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
to define in onCreate()
which the orientation should be vertical and I also declare this variable boolean orienta;
whose value is true
And with this method I determine if the screen is vertical and then it goes to horizontal or vice versa
private void oree(){
if(orienta){
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
orienta=false;
}else{
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
orienta=true;
}
}
But all it does is update the activity and it doesn't rotate it.
If you know anything about this topic, I will be happy to hear from you. Thanks.
What you want is to change from one orientation to another, I think it is advisable to define an orientation for your Activity instead of changing to a desired state.
You can define it as vertical (portrait):
programmatically :
or as horizontal (landscape):
programmatically:
If you don't want the to be created again
Activity
, you can set the propertyandroid:configChanges
to this value:with this you will prevent it
Activity
from being created again when rotating, you will notice that it will not enter again to call the methodonCreate()
.Another way to treat the rotation is to allow the creation of the activity and with the flag
savedInstanceState
you can determine if it is the first time it is created or if it has already been created.Previously the data must be saved in
onSaveInstanceState
exampleTo retrieve the value
Properties with
static
are kept in memory when rotating the device.