Android/Kotlin - 关闭后恢复应用时变老 Activity
Android/Kotlin - get old Activity when resume app after close
我的问题是:
我使用 finish()
关闭 Activity,它会转到 onPause -> onStop -> onDestroy。
接下来我打开应用程序,onCreate()
获取对所有视图和 context
的旧引用。
当我尝试显示简单对话框时,它抛出:
"Unable to add window -- token android.os.BinderProxy@69a156a is not
valid; is your activity running?"
我也无法访问文本视图
progressText?.text = message
它得到了旧的参考 - 我使用了 clearFindViewByIdCache() - 但没有效果。
怎么了?
编辑
我尝试通过 DataSyncListener 方法 runOnUiThread 来操作视图
class MainActivity : AppCompatActivity(), DataSyncListener {
override fun onSuccess() {
runOnUiThread {
refreshLayout?.isRefreshing = false // it DO NOT works after reopen app,
syncProgressText?.visibility = View.GONE // it DO NOT works after reopen app,
}
}
override fun onFailure() {
runOnUiThread {
refreshLayout?.isRefreshing = false // it DO NOT works after reopen app,
syncProgressText?.visibility = View.GONE // it DO NOT works after reopen app,
}
}
override fun onError(message: String) {
Logger.d(message)
runOnUiThread {
refreshLayout?.isRefreshing = false // it DO NOT works after reopen app
syncProgressText?.visibility = View.GONE // it DO NOT works after reopen app
displayInfoAlertWithConfirm(this@MainActivity, message, DialogInterface.OnClickListener { _, _ -> // it DO NOT works after reopen app, throws Unable to add window
refreshLayout?.isRefreshing = true // it DO NOT works after reopen app
syncProgressText?.visibility = View.VISIBLE // it DO NOT works after reopen app
})
}
}
override fun onProgress(message: String) {
runOnUiThread {
syncProgressText?.text = message // it DO NOT works after reopen app
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
refreshLayout.setOnRefreshListener({
// it DO NOT works after reopen app,
synchronizeData()
})
synchronizeData()
syncProgressText?.text = "test" // it works after reopen app
}
override fun onPostCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
super.onPostCreate(savedInstanceState, persistentState)
actionBarDrawerToggle?.syncState()
}
fun synchronizeData() {
refreshLayout?.isRefreshing = true
dataSynchronizer = DataSynchronizer.getInstance(application as MyApplication?, this)
dataSynchronizer?.startSync() // background featch data
syncProgressText?.visibility = View.VISIBLE // it DO NOT works after reopen app
}
override fun onDestroy() {
super.onDestroy()
dataSynchronizer?.stopSync() // kill background task
clearFindViewByIdCache() // no effect
}
}
EDIT2
已修复 - DataSynchronizer 不是 GC 并保留旧引用
使用 syncProgressText.setText(消息),syncProgressText.text 需要可编辑,而不是字符串。
终于修复。感谢@Viktor,在检查了我的 DataSynchronizer.getInstance(应用程序作为 MyApplication?,这个)后,我意识到 DataSynchronizer 不是 GC——内存泄漏。所以它保留了旧的引用。现在它就像一个魅力。
我的问题是:
我使用 finish()
关闭 Activity,它会转到 onPause -> onStop -> onDestroy。
接下来我打开应用程序,onCreate()
获取对所有视图和 context
的旧引用。
当我尝试显示简单对话框时,它抛出:
"Unable to add window -- token android.os.BinderProxy@69a156a is not valid; is your activity running?"
我也无法访问文本视图
progressText?.text = message
它得到了旧的参考 - 我使用了 clearFindViewByIdCache() - 但没有效果。
怎么了?
编辑
我尝试通过 DataSyncListener 方法 runOnUiThread 来操作视图
class MainActivity : AppCompatActivity(), DataSyncListener {
override fun onSuccess() {
runOnUiThread {
refreshLayout?.isRefreshing = false // it DO NOT works after reopen app,
syncProgressText?.visibility = View.GONE // it DO NOT works after reopen app,
}
}
override fun onFailure() {
runOnUiThread {
refreshLayout?.isRefreshing = false // it DO NOT works after reopen app,
syncProgressText?.visibility = View.GONE // it DO NOT works after reopen app,
}
}
override fun onError(message: String) {
Logger.d(message)
runOnUiThread {
refreshLayout?.isRefreshing = false // it DO NOT works after reopen app
syncProgressText?.visibility = View.GONE // it DO NOT works after reopen app
displayInfoAlertWithConfirm(this@MainActivity, message, DialogInterface.OnClickListener { _, _ -> // it DO NOT works after reopen app, throws Unable to add window
refreshLayout?.isRefreshing = true // it DO NOT works after reopen app
syncProgressText?.visibility = View.VISIBLE // it DO NOT works after reopen app
})
}
}
override fun onProgress(message: String) {
runOnUiThread {
syncProgressText?.text = message // it DO NOT works after reopen app
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
refreshLayout.setOnRefreshListener({
// it DO NOT works after reopen app,
synchronizeData()
})
synchronizeData()
syncProgressText?.text = "test" // it works after reopen app
}
override fun onPostCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
super.onPostCreate(savedInstanceState, persistentState)
actionBarDrawerToggle?.syncState()
}
fun synchronizeData() {
refreshLayout?.isRefreshing = true
dataSynchronizer = DataSynchronizer.getInstance(application as MyApplication?, this)
dataSynchronizer?.startSync() // background featch data
syncProgressText?.visibility = View.VISIBLE // it DO NOT works after reopen app
}
override fun onDestroy() {
super.onDestroy()
dataSynchronizer?.stopSync() // kill background task
clearFindViewByIdCache() // no effect
}
}
EDIT2
已修复 - DataSynchronizer 不是 GC 并保留旧引用
使用 syncProgressText.setText(消息),syncProgressText.text 需要可编辑,而不是字符串。
终于修复。感谢@Viktor,在检查了我的 DataSynchronizer.getInstance(应用程序作为 MyApplication?,这个)后,我意识到 DataSynchronizer 不是 GC——内存泄漏。所以它保留了旧的引用。现在它就像一个魅力。