简体   繁体   中英

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

My problem is:

I close Activity using finish() , it goes to onPause -> onStop -> onDestroy . Next I open app, onCreate() gets old references to all views and 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?"

I also cannot access text view

progressText?.text = message

it gets old reference - i used clearFindViewByIdCache() -- but no effect.

What's wrong?

EDIT

I try to manipulate views from DataSyncListener methods 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 was not GC and hold old references

使用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. So it holded old refrerences. Now it works like a charm.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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