简体   繁体   中英

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 !!).

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.

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.

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 !!).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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