繁体   English   中英

Android / Kotlin-关闭后恢复应用程序时出现旧活动

[英]Android/Kotlin - get old Activity when resume app after close

我的问题是:

我使用finish()关闭Activity,它转到onPause-> onStop-> onDestroy 接下来,我打开应用程序, onCreate()获取所有视图和context旧引用。

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

“无法添加窗口-令牌android.os.BinderProxy@69a156a无效;您的活动正在运行吗?”

我也无法访问文本视图

progressText?.text =消息

它得到了旧的参考-我使用了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
}

}

编辑2

修复 -DataSynchronizer不是GC并保留旧的引用

使用syncProgressText.setText(message),syncProgressText.text期望可编辑,而不是字符串。

终于修复了 感谢@Viktor,在检查了我的DataSynchronizer.getInstance(application as MyApplication ?, this)之后,我意识到DataSynchronizer不是GC-内存泄漏。 因此它拥有旧的参考文献。 现在,它就像一种魅力。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM