问题
I have an app that I set to only display in Portrait mode by setting
android:screenOrientation="portrait"
for each Activity in the manifest. This works fine and my app screen does not rotate when I rotate the phone.
The problem is, if I start the app with the phone in Portrait orientation, then as my first Activity runs (an animated splash screen) and I rotate the phone to Landscape while the first Activity is still running, when the next activity is launched (it is launched from the finally block of the first activity's thread) the app will quit. It does not force quit, it simply exits to the home screen. There are no exceptions thrown or anything else in LogCat that tells why this is happening. The screen never rotates (as it shouldn't). If I start the app with the phone in Landscape it works fine. This only happens when I start in Portrait and rotate to Landscape after.
I have tried putting
android:configChanges="orientation"
in every activity and I have also tried
android:configChanges="orientation|keyboard|keyboardHidden|screenLayout"
I tried the above with and without implementing onConfigurationChanged in the activities. When I did implement it I simply ignored it (just calling super.onConfigurationChanged).
I have also tried programatically setting the activity to Portrait orientation in addition to setting it in the manifest.
I can also get this to happen on the original Motorola Droid by starting the app with the physical keyboard open and starting the app in the car dock. The app starts, splash screen starts in Portrait orientation (even though the keyboard is open or the phone is in car dock) but as soon as that second activity is started the app quits.
I don't think this has anything to do with the Activity being destroyed on an orientation change. All my global data is stored in my app's custom Application class and as I mentioned before, I get no exceptions such as a null pointer in the case of a global getting wiped out.
I am running a Motorola Droid original on Verizon with Android 2.3.2.
回答1:
i would recommend to set the orientation inside your splash screen or the next activity programatically. Try to implement this code:
//get the WindowManager Service
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
//get the orientation
int orientation = display.getOrientation();
//orientation % 2 == 0 is portrait mode,
if ((orientation % 2) != 0) (android.view.IWindowManager.Stub.asInterface(ServiceManager.getService("window"))).setOrientation(0);
I hope this will work.
回答2:
The application closes on orientation change because you must provide with particular resources in the res folder
- Eg:layout-land in res folder or
handle the change at runtime through programmatically
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}
来源:https://stackoverflow.com/questions/5148259/android-screen-orientation-change-causes-app-to-quit