に
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
setContentView(R.layout.lanscape_activity);
// your own layout
} else {
setContentView(R.layout.portrait_activity);
// your own layout
}
}
そして、この行を、このメソッドを追加します。したがって、あなたはそれを自分で処理しなければなりません。
onSaveInstanceState()
で現在の状態を保存:
override fun onSaveInstanceState(outState: Bundle?) {
super.onSaveInstanceState(outState)
outState.putBoolean("someKey", constraintLayout.visibility == View.VISIBLE)
}
その後、onCreate()
に:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Initialize views
...
// if `savedInstanceState` is not null, then look for `someKey` in bundle
savedInstanceState?.run {
val isVisible = getBoolean("someKey")
constraintLayout.visibility = if (isVisible) View.VISIBLE else View.INVISIBLE
}
if (null == savedInstanceState) {
// This activity is starting for the first time,
// not an orientation change case
}
}
参照[構成の変更の取り扱い](https://developer.android.com/guide/topics/resources /runtime-changes.html) – santalu