Below is my Activity where I need to handle orientation change differently, basically whenever onConfigurationChanged() event is fired due to device orientation change, I show a dialog window and ask user whether he wants to update the orientation, based on users action i update to landscape/portrait or keep the same orientation.
public class MyActivity extends Activity {
..
..
private boolean mFlagConfigChangeManual = false;
@Override
public void onConfigurationChanged(Configuration newConfig) {
Log.e("onConfigurationChanged", "mFlagConfigChangeManual="+mFlagConfigChangeManual);
super.onConfigurationChanged(newConfig);
int orient = -1;
if (!mFlagConfigChangeManual) {
if (newConfig.orientation == newConfig.ORIENTATION_LANDSCAPE) {
mFlagConfigChangeManual = true;
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
orient = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
} else if (newConfig.orientation == newConfig.ORIENTATION_PORTRAIT) {
mFlagConfigChangeManual = true;
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
orient = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
}
} else {
mFlagConfigChangeManual = false;
return;
}
final int orient2 = orient;
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
if (orient2 >= 0) {
mFlagConfigChangeManual = true;
setRequestedOrientation(orient2);
}
break;
case DialogInterface.BUTTON_NEGATIVE:
break;
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Would you like to update orientation?")
.setPositiveButton("Yes", dialogClickListener)
.setNegativeButton("No", dialogClickListener).show();
}
I have made the required changes in manifest file but I face two issues, first time orientation confirmation dialog is displayed it works but when I re-orient again, it does not do anything, basically onConfigurationChanged() is not called another time. Another issue I have noticed is;
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); method orients the view upside down when landscape mode. Can this be done better way?