Android / Kotlin - 关闭后恢复应用时获取旧活动

时间:2018-02-28 10:47:22

标签: android kotlin kotlin-android-extensions

我的问题是:

我使用finish()关闭活动,转到 onPause - > onStop - >的onDestroy 即可。 接下来,我打开应用,onCreate()获取所有视图和context的旧引用。

当我尝试显示简单对话框时,它会抛出:

  

"无法添加窗口 - 令牌android.os.BinderProxy@69a156a不是   有效;你的活动在运行吗?"

我也无法访问文字视图

  

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

FIXED - DataSynchronizer不是GC并保留旧引用

2 个答案:

答案 0 :(得分:0)

使用syncProgressText.setText(message),syncProgressText.text需要Editable,而不是String。

答案 1 :(得分:0)

最后修复。感谢@Viktor,在检查了我的DataSynchronizer.getInstance(应用程序为MyApplication?,这个)后,我意识到DataSynchronizer不是GC - 内存泄漏。所以它坚持旧的反思。现在它就像一个魅力。