简体   繁体   English

方向改变时杀死活动

[英]Kill an activity when orientation changes

I would like to know if it is possible to ask Android not to reload one Activity when orientation changes (I want to reload others but to kill this one !!). 我想知道是否有可能要求Android在方向更改时不重新加载一个Activity(我想重新加载其他人但是要杀死这个!!)。

I have checked the activity properties that I can set in the manifest but no one seems to allow that. 我已经检查了我可以在清单中设置的活动属性,但似乎没有人允许这样做。

Thanks ! 谢谢 !

Try this: 尝试这个:

public boolean onRetainNonConfigurationInstance() {
    return true;
}

public onCreate() {
    if(getLastNonConfigurationInstance() != null) {
        finish();
    }
}

It is simple, in your activity override onConfigurationChanged(Configuration config) and kill the activity inside of that. 很简单,在你的活动中覆盖onConfigurationChanged(配置配置)并杀死其中的活动。

Ex: 例如:

    @Override
    public void onConfigurationChanged (Configuration newConfig)
    {
          super.onConfigurationChanged(newConfig);
          finish();
    }

This will cause the activity to be killed when a configuration change takes place. 这将导致在发生配置更改时终止活动。 In the manifest under config changes select orientation for your activity. 配置更改下的清单中,选择您的活动的方向。 You can check which type of orientation it is changing to by looking at newConfig.orientation and compare it to the constants for portrait and landscape in the Configuration class. 您可以通过查看newConfig.orientation并将其与Configuration类中的纵向和横向常量进行比较来检查它所更改的方向类型。

I would like to know if it is possible to ask Android not to reload one Activity when orientation changes (I want to reload others but to kill this one !!). 我想知道是否有可能要求Android在方向更改时不重新加载一个Activity(我想重新加载其他人但是要杀死这个!!)。

Possible? 可能? Sure. 当然。 A good idea? 一个好主意? No. Users will get very confused if a simple twist of their wrist causes the current activity to go away. 不会。如果手腕的简单扭曲导致当前活动消失,用户将会感到非常困惑。 They might think that your app is broken. 他们可能会认为您的应用已损坏。

Ideally, you allow users to use your activities in any orientation they desire. 理想情况下,您允许用户以他们想要的任何方向使用您的活动。 It is their device, not yours. 这是他们的设备,而不是你的设备。

If, for some reason, some activity only makes sense in one orientation (eg, landscape), use the android:orientation attribute in the <activity> element in the manifest to keep it in that orientation. 如果由于某种原因,某些活动仅在一个方向(例如,横向)中有意义,请使用清单中<activity>元素中的android:orientation属性将其保持在该方向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM