[英]What is the best way of creating a reusable dialog with Activity's lifecycle?
[英]What is the best way to declare a variable for a TextView that changes during lifecycle of Activity?
我決定通過在循環的每個階段更改簡單的 TextView 的不同屬性來了解 Activity 的生命周期是如何工作的。 我想知道為上述 TextView 聲明變量的最佳方法是什么,以便我可以在每個階段使用它。 我當前的解決方案是使用lateinit var
聲明一個變量,並在onCreate
中分配一個值
private lateinit var helloView: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
helloView = findViewById(R.id.helloText)
helloView.text = "foo"
}
override fun onPause() {
super.onPause()
helloView.setTextColor(Color.RED)
}
override fun onRestart() {
super.onRestart()
helloView.text = "bar"
}
一種更簡單的方法是改用 Logcat。 在生命周期的每個階段都使用它和一條消息,並檢查何時顯示每條消息。 通過您的嘗試,如果您只想使用這些生命周期階段(例如 onStop 或 onDestroy),則很難看到變化。
閱讀本指南也是一個好的開始https://developer.android.com/guide/components/activities/activity-lifecycle
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.