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

问题描述 投票:0回答:2

我的问题是:

我使用finish()关闭Activity,它转到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
}

}

Aaditi

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

android kotlin kotlin-android-extensions
2个回答
0
投票

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


0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.