我面临一个奇怪的问题,其中我的活动被黑屏卡住,生命周期中的最后一个回调是onStop()
。
流为:
第3步之后,该错误就会出现。
在第3步之后,在我测试过的所有API中
onStop()
。这是我正在使用的两个活动的代码。 完整的项目位于my GitHub Repository中。
MainActivity.kt
package com.gustavogoma.android.test.testingissue3759
import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
Log.i(TAG, "onCreate")
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
if (shouldOpenAnotherActivity) {
shouldOpenAnotherActivity = false
startActivity(Intent(this, AnotherActivity::class.java))
}
}
override fun onRestart() {
Log.i(TAG, "onRestart")
super.onRestart()
}
override fun onStart() {
Log.i(TAG, "onStart")
super.onStart()
}
override fun onResume() {
Log.i(TAG, "onResume")
super.onResume()
}
override fun onPause() {
Log.i(TAG, "onPause")
super.onPause()
}
override fun onStop() {
Log.i(TAG, "onStop")
super.onStop()
}
override fun onDestroy() {
Log.i(TAG, "onDestroy")
super.onDestroy()
}
companion object {
private val TAG = MainActivity::class.java.simpleName
private var shouldOpenAnotherActivity = true
}
}
AnotherActivity.kt
package com.gustavogoma.android.test.testingissue3759
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import androidx.appcompat.app.AppCompatDelegate
import kotlinx.android.synthetic.main.activity_another.*
class AnotherActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_another)
Handler(Looper.getMainLooper()).postDelayed(
{
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
finish()
},
5000
)
}
}
MainActivity logcat(Android 16):
11-26 17:03:45.806 2051-2051 I/MainActivity: onCreate
11-26 17:03:46.098 2051-2051 I/MainActivity: onStart
11-26 17:03:46.101 2051-2051 I/MainActivity: onResume
11-26 17:03:46.114 2051-2051 I/MainActivity: onPause
11-26 17:03:46.344 2051-2051 I/MainActivity: onStop
11-26 17:03:51.310 2051-2051 I/MainActivity: onRestart
11-26 17:03:51.311 2051-2051 I/MainActivity: onStart
11-26 17:03:51.312 2051-2051 I/MainActivity: onResume
11-26 17:03:51.354 2051-2051 I/MainActivity: onPause
11-26 17:03:51.355 2051-2051 I/MainActivity: onStop
11-26 17:03:51.355 2051-2051 I/MainActivity: onDestroy
11-26 17:03:51.357 2051-2051 I/MainActivity: onCreate
11-26 17:03:51.383 2051-2051 I/MainActivity: onStart
11-26 17:03:51.383 2051-2051 I/MainActivity: onResume
11-26 17:03:51.392 2051-2051 I/MainActivity: onPause
11-26 17:03:51.393 2051-2051 I/MainActivity: onStop