我有MainActivity,它在清单中具有固定的方向
<activity
android:name=".MainActivity"
android:configChanges="orientation|screenSize"
android:screenOrientation="portrait"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
MainActivity.kt
class MainActivity : LifecyclerLoggerActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
findViewById<Button>(R.id.button).setOnClickListener({ startSecondActivity() })
}
private fun startSecondActivity() {
startActivity(Intent(this, SecondActivity::class.java))
}
override fun onConfigurationChanged(newConfig: Configuration?) {
super.onConfigurationChanged(newConfig)
if (newConfig?.orientation == Configuration.ORIENTATION_PORTRAIT) {
Log.d("MainActivity", "onConfChanged orientation is portrait" )
} else if (newConfig?.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Log.d("MainActivity", "onConfChanged orientation is landscape" )
}
}
}
按钮启动SecondActivity,该清单在清单中具有固定的横向方向:
<activity
android:launchMode="singleTop"
android:name=".SecondActivity"
android:configChanges="orientation|screenSize"
android:screenOrientation="sensorLandscape" />
SecondActivity.kt
class SecondActivity : LifecyclerLoggerActivity(){
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.second_activity)
}
override fun onResume() {
super.onResume()
Handler().postDelayed({finish()}, 200)
}
}
“主要活动”和“第二活动”都扩展了LifecyclerLoggerActivity,该活动记录了生命周期方法调用。
SecondActivity的OnResume方法在200ms之后完成它。然后MainActivity恢复方向更改回纵向。
那是什么问题?
在调用finish()之后,MainActivity会恢复为LANDSCAPE模式约300 ms,然后将方向更改为纵向,这看起来确实很丑。
Logcat的日志:
D/LifecycleLogger: On Pause of MainActivity215908412
D/LifecycleLogger: On Create of SecondActivity123929575
D/LifecycleLogger: On Resume of SecondActivity123929575
D/LifecycleLogger: On Stop of MainActivity215908412
D/LifecycleLogger: On Pause of SecondActivity123929575
D/MainActivity: onConfChanged orientation is landscape
D/LifecycleLogger: On Resume of MainActivity215908412
D/MainActivity: onConfChanged orientation is portrait
D/LifecycleLogger: On Stop of SecondActivity123929575
D/LifecycleLogger: On Destroy of SecondActivity123929575
那么,如果MainActivity的配置固定为纵向,为什么还要以横向模式恢复呢?