您的位置:首页 > 其它

AsyncTask和Activity的生命周期方法

2012-07-13 17:10 197 查看
Life Cycle Methods and AsyncTask

What will happen if a user clicks the Back button when a progress dialog initiated by an

async task is visible? This will cancel the dialog as per UI guidelines. However, if you

don’t take the precaution of al so cancelling the async task, it will continue to run. So a

good practice is to capture the oncancel of the dialog and exp licitly cancel the async

task.

What happens if you are not using a progress dialog but are the progress through some

other means on the activity? What happens if a user navigates away from the activity

either through a Back button or a press on the Home key? In both these cases, there is

no expectation that the user will come back any time soon. Many times, the right thing

to do is recognize this life cycle state of the activity and then accordingly cancel the

async task.

In short, an async task needs to be fully aware of the life cycle states of the activity. This

may behoove you to actually implement life cycle methods on your task and have the

activity call these life cycle methods so that an async task behaves as if it is a part and

parcel of the activity.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: