android:configChanges属性

设置Activity的android:configChanges时,切屏会重新调用各个生命周期

本人实际测试发现设置

android:configChanges="keyboardHidden|orientation"或不设置
横竖屏切换时也会执行onCreate各一次,并且不会执行onConfigurationChanged
设置
android:configChanges="orientation|screenSize"
不会执行onCreate,会执行onConfigurationChanged。
测试代码:

public class MainActivity extends Activity {

    TextView lbl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lbl = (TextView)findViewById(R.id.label);
        Log.d("123", "onCreate");
    }

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

        if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){
            lbl.setText("横屏");
        }
        else{
            lbl.setText("竖屏");
        }
    }

}

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

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>


你可能感兴趣的:(android:configChanges属性)