简体   繁体   中英

Is that right that a short running Android background task can safely be ignored even if the caller Activity is destroyed?

I'm thinking about error handling of the background task of an activty and it seems I don't have to do anything special, because if the activity is destroyed while the background task is running (for a few seconds at most) and the task tries to access the activity UI when it finished then it may get an exception, because the UI does not exist anymore, but it's a background task, so the user won't see it anyway.

So I can safely ignore if the background task is still running, because it may stopped with an exception, but the user won't notice it. Is that right?

Ideally its not right. Theoretically you should close/cancel all the background tasks that activity has started and does not bother about the results from those tasks. You can do that in onDestroy() .

Rest its upto programmer that if he wants to do things like

try{
//Access UI stuff
}
catch(Exception e)
{
// Dont even print the exception
}

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