1
0
mirror of https://github.com/etesync/android synced 2025-02-02 19:01:06 +00:00

AsyncTask: cancel background tasks when the fragments are destroyed.

I assumed the lifecycle of the fragment and the task were tied because they
are tied to the instance, but it looks like I was wrong. We need to
explicitly cancel tasks.
This commit is contained in:
Tom Hacohen 2017-05-12 20:28:45 +01:00
parent 28aa80fe07
commit 30fa0128b6
3 changed files with 28 additions and 4 deletions

View File

@ -40,6 +40,7 @@ public class CollectionMembersListFragment extends ListFragment implements Adapt
private Account account;
private CollectionInfo info;
private JournalEntity journalEntity;
private AsyncTask asyncTask;
private TextView emptyTextView;
@ -73,7 +74,7 @@ public class CollectionMembersListFragment extends ListFragment implements Adapt
@Override
public void refresh() {
new JournalMembersFetch().execute();
asyncTask = new JournalMembersFetch().execute();
}
@Override
@ -85,6 +86,13 @@ public class CollectionMembersListFragment extends ListFragment implements Adapt
getListView().setOnItemClickListener(this);
}
@Override
public void onDestroyView() {
super.onDestroyView();
if (asyncTask != null)
asyncTask.cancel(true);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final JournalManager.Member member = (JournalManager.Member) getListAdapter().getItem(position);

View File

@ -166,6 +166,7 @@ public class JournalItemActivity extends BaseActivity implements Refreshable {
public static class PrettyFragment extends Fragment {
CollectionInfo info;
SyncEntry syncEntry;
private AsyncTask asyncTask;
public static PrettyFragment newInstance(CollectionInfo info, SyncEntry syncEntry) {
PrettyFragment frag = new PrettyFragment();
@ -186,17 +187,24 @@ public class JournalItemActivity extends BaseActivity implements Refreshable {
switch (info.type) {
case ADDRESS_BOOK:
v = inflater.inflate(R.layout.contact_info, container, false);
new LoadContactTask(v).execute();
asyncTask = new LoadContactTask(v).execute();
break;
case CALENDAR:
v = inflater.inflate(R.layout.event_info, container, false);
new LoadEventTask(v).execute();
asyncTask = new LoadEventTask(v).execute();
break;
}
return v;
}
@Override
public void onDestroyView() {
super.onDestroyView();
if (asyncTask != null)
asyncTask.cancel(true);
}
private class LoadEventTask extends AsyncTask<Void, Void, Event> {
View view;
LoadEventTask(View v) {

View File

@ -41,6 +41,7 @@ public class ListEntriesFragment extends ListFragment implements AdapterView.OnI
private EntityDataStore<Persistable> data;
private CollectionInfo info;
private JournalEntity journalEntity;
private AsyncTask asyncTask;
private TextView emptyTextView;
@ -74,11 +75,18 @@ public class ListEntriesFragment extends ListFragment implements AdapterView.OnI
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
new JournalFetch().execute();
asyncTask = new JournalFetch().execute();
getListView().setOnItemClickListener(this);
}
@Override
public void onDestroyView() {
super.onDestroyView();
if (asyncTask != null)
asyncTask.cancel(true);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
EntryEntity entry = (EntryEntity) getListAdapter().getItem(position);