Android configuration changed

2019-08-05 10:06发布

I'm interested to know which methods are overridden, when an Android device is rotated (i.e. when configuration changes)?

onSaveInstanceState(...), onConfigurationChanged(...), onRestoreInstanceState(...) - something similar to this?

It will be also interesting for me to listen about the whole process connected with changing a configuration. Thanks.

4条回答
对你真心纯属浪费
2楼-- · 2019-08-05 10:36

According to the android developer reference you have to use:

@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();
    }
}

For more information see: http://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange

You could be interested in seeing: http://developer.android.com/guide/topics/manifest/activity-element.html#config

Here you can find all configuration listener you can listen.

Remember to put them in the android manifest:

<activity 
   android:configChanges=["orientation"]
   . . .
</activity>

Hope this helps...

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-08-05 10:41

Go to Manifest and add: android:configChanges="orientation|screenSize"

 <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"
                    android:configChanges="orientation|screenSize"
                    />

In Main Activity: Copy and Paste this code:

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


    }

All Done -

查看更多
欢心
4楼-- · 2019-08-05 10:54

From android developers...

To retain an Object during a runtime configuration change:

  1. Override the onRetainNonConfigurationInstance() method to return the Object you would like to retain.
  2. When your Activity is created again, call getLastNonConfigurationInstance() to recover your Object.

Android calls onRetainNonConfigurationInstance() between onStop() and onDestroy() when it shuts down your Activity due to a configuration change. In your implementation of onRetainNonConfigurationInstance(), you can return any Object that you need in order to efficiently restore your state after the configuration change.

A scenario in which this can be valuable is if your application loads a lot of data from the web. If the user changes the orientation of the device and the Activity restarts, your application must re-fetch the data, which could be slow. What you can do instead is implement onRetainNonConfigurationInstance() to return an object carrying your data and then retrieve the data when your Activity starts again with getLastNonConfigurationInstance().

Look at here for more info Retaining an Object During a Configuration Change you can also find example over their..

Thanks..

查看更多
干净又极端
5楼-- · 2019-08-05 11:01

When you rotate the device your Activity will re-create and all the variables will be re-initialized. So, in that case if you want to some values to remain same on Rotation also you can store their state using the onSaveInstanceState() and you can restore in onCreate() again by checking Bundle is not null.

if(savedInstanceState != null){
            // get the restore value from the Bundle
        }

Where as onConfigurationChanged() will be called when you rotate the Device(Note that this will only be called if you have selected configurations you would like to handle with the configChanges attribute in your manifest). From the parameter you will get the new device configuration.

If you don't want your Activity to get re-created on rotation of Device then you have to add this line to your activity tag in the AndroidManifest file.

android:ConfigChanges="keyboardHidden|orientation"
查看更多
登录 后发表回答