简体   繁体   中英

proper way to know if an activity has been finished

I've read

" How to know activity has been finished? " and " Proper way to know whether an Activity has been destroyed "

but none of them got real answers

I've a background task updating a screen with it's progress

The user has a button to cancel the background task at any moment, and if he does that the background task will be stoped and activity will be finished...

BUT as all of this happen in an asynchronous enviroment the following situation may happen:

1- the background task stacks some notification to update progress activity
2- the user cancel the background task
3- the background task is stopped (i mean, stops having progress) and activity is finished ( activity.finish() )
4- the previous stacked updates are delivered to the activity which tries to perform some update on its fields and lead to error

I would like an "oficial android approach" better than having a boolean which is set to true during onDestroy()

Since you're finishing the activity by calling finish() then I think checking isFinishing() before updating the UI would work in your use case.

For more advanced use cases I suggest you should look into RxJava for doing asynchronous background tasks that are tightly coupled with activity lifecycle.

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