简体   繁体   English

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

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

My problem is: 我的问题是:

I close Activity using finish() , it goes to onPause -> onStop -> onDestroy . 我使用finish()关闭Activity,它转到onPause-> onStop-> onDestroy Next I open app, onCreate() gets old references to all views and context . 接下来,我打开应用程序, onCreate()获取所有视图和context旧引用。

When I try show simple dialog it throws: 当我尝试显示简单对话框时,它抛出:

"Unable to add window -- token android.os.BinderProxy@69a156a is not valid; is your activity running?" “无法添加窗口-令牌android.os.BinderProxy@69a156a无效;您的活动正在运行吗?”

I also cannot access text view 我也无法访问文本视图

progressText?.text = message progressText?.text =消息

it gets old reference - i used clearFindViewByIdCache() -- but no effect. 它得到了旧的参考-我使用了clearFindViewByIdCache()-但没有效果。

What's wrong? 怎么了?

EDIT 编辑

I try to manipulate views from DataSyncListener methods runOnUiThread 我尝试从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 编辑2

FIXED - DataSynchronizer was not GC and hold old references 修复 -DataSynchronizer不是GC并保留旧的引用

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

Finally fixed . 终于修复了 Thanks @Viktor, after checked my DataSynchronizer.getInstance(application as MyApplication?, this) I realized that DataSynchronizer was not GC -- memory leaks. 感谢@Viktor,在检查了我的DataSynchronizer.getInstance(application as MyApplication ?, this)之后,我意识到DataSynchronizer不是GC-内存泄漏。 So it holded old refrerences. 因此它拥有旧的参考文献。 Now it works like a charm. 现在,它就像一种魅力。

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

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