Android, how to not destroy the activity when I ro

2019-01-04 22:04发布

I have an app that works only in portrait mode, and I have made the changes in my manifest file for every activity the orientation to be portrait. But when I rotate the device, the activity recreates again. How to not destroy the activity?

6条回答
【Aperson】
2楼-- · 2019-01-04 22:38

For API 12 and below: add

android:configChanges="orientation"

Add "screenSize" if you are targeting API 13 or above because whenever your orientation changes so does your screen size, otherwise new devices will continue to destroy your activity. See Egg's answer below for more information on using "screenSize"

android:configChanges="orientation|screenSize"

to your Activity in AndroidManifest.xml. This way your Activity wont be restarted automatically. See the documentation for more infos

查看更多
Explosion°爆炸
3楼-- · 2019-01-04 22:39

From the official document flurin said,

Note: If your application targets API level 13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), then you should also declare the "screenSize" configuration, because it also changes when a device switches between portrait and landscape orientations.

So if your app targets API level 13 or higher, you should set this config instead:

android:configChanges="orientation|screenSize"

查看更多
小情绪 Triste *
4楼-- · 2019-01-04 22:46

write in manifest:

android:configChanges="orientation|screenSize|keyboardHidden"

and override this in activity that solved your problem:

 @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
}
查看更多
姐就是有狂的资本
5楼-- · 2019-01-04 22:47

The right solution is

android:configChanges="orientation|screenSize"

Android documentation:

The current available screen size has changed. This represents a change in the currently available size, relative to the current aspect ratio, so will change when the user switches between landscape and portrait. However, if your application targets API level 12 or lower, then your activity always handles this configuration change itself (this configuration change does not restart your activity, even when running on an Android 3.2 or higher device).

查看更多
够拽才男人
6楼-- · 2019-01-04 22:47

I was messing this up for a little bit and then relized that inside the Manifest file I was putting the configChanges on the Application level and not on the Activity Level. Here is what the code looks like when it is correctly working for me.

Image of correct manifest files code

查看更多
等我变得足够好
7楼-- · 2019-01-04 22:54

Look at this code in Floating Image. It has the most interesting way of handling screen rotation ever. http://code.google.com/p/floatingimage/source/browse/#svn/trunk/floatingimage/src/dk/nindroid/rss/orientation

查看更多
登录 后发表回答