Android : Setting Android Activity Orientation and lock it during onStart

on Thursday, April 16, 2015


I have an activity that I want to lock the orientation of it based on the orientation of the device when the activity starts, I can't seem to figure it out. The code I have now is



protected void onStart() {
super.onStart();

int currentOrientation = getResources().getConfiguration().orientation;
if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
}
else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
}
doStuffBasedOnOrientation();

}


It seems like it would only work for the first onStart then it will stop changing.


0 comments:

Post a Comment