----------------------------------------------
//设置回调接口的Fragment
public class TestFragment extends Fragment {
//启动异步任务
private void methodThatStartsTheAsyncTask() {
TestAsyncTask testAsyncTask = new TestAsyncTask(new FragmentCallback() {
@Override
//执行回调操作
public void onTaskDone() {
methodThatDoesSomethingWhenTaskIsDone();
}
});
}
private void methodThatDoesSomethingWhenTaskIsDone() {
//异步任务完成后的操作
}
//异步任务回调Fragment接口
public interface FragmentCallback {
public void onTaskDone();
}
}
-----------------------------------------------------
public class TestAsyncTask extends AsyncTask<Void, Void, Void> {
private FragmentCallback mFragmentCallback;
public TestAsyncTask(FragmentCallback fragmentCallback) {
mFragmentCallback = fragmentCallback;
}
@Override
protected Void doInBackground(Void... params) {
//异步执行的任务
return null;
}
//执行后回调操作
@Override
protected void onPostExecute(Void result) {
mFragmentCallback.onTaskDone();
}
}
-------------------------------------------------------------------------