I'm developing a mobile application based on Android with minSdkVersion=15
. I would like to support both orientations for tablets and only portrait for smartphones. Everything works like a charm but I'm experiencing a little bug that is driving me crazy.
When smartphone is in landscape mode and I try to trigger a new Activity, it opens in landscape mode for a while and then autorotates to portrait.
Each one of my activities extend a GeneralActivity class:
public class GeneralActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// If smartphone lock orientation to portrait
if (!Helper.isTablet(this.getApplicationContext())){
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
}
I detect tablets with this function:
public class Helper {
public static boolean isTablet(Context context){
Configuration config = context.getResources().getConfiguration()
return config.smallestScreenWidthDp >= 600;
}
}
I choose not to specify android:screenOrientation
inside Manifest.xml because in that way I'm able to support all interface orientation for tablets.
Am I missing something?
EDIT
I decided to apply the best practice suggested in the answer by Jonathan, but the issue I described is still here. Here's my repo on github: https://github.com/giacmarangoni/Android-Orientation-Test