mirror of
https://github.com/etesync/android
synced 2025-02-09 06:02:42 +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:
parent
28aa80fe07
commit
30fa0128b6
@ -40,6 +40,7 @@ public class CollectionMembersListFragment extends ListFragment implements Adapt
|
|||||||
private Account account;
|
private Account account;
|
||||||
private CollectionInfo info;
|
private CollectionInfo info;
|
||||||
private JournalEntity journalEntity;
|
private JournalEntity journalEntity;
|
||||||
|
private AsyncTask asyncTask;
|
||||||
|
|
||||||
private TextView emptyTextView;
|
private TextView emptyTextView;
|
||||||
|
|
||||||
@ -73,7 +74,7 @@ public class CollectionMembersListFragment extends ListFragment implements Adapt
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void refresh() {
|
public void refresh() {
|
||||||
new JournalMembersFetch().execute();
|
asyncTask = new JournalMembersFetch().execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -85,6 +86,13 @@ public class CollectionMembersListFragment extends ListFragment implements Adapt
|
|||||||
getListView().setOnItemClickListener(this);
|
getListView().setOnItemClickListener(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDestroyView() {
|
||||||
|
super.onDestroyView();
|
||||||
|
if (asyncTask != null)
|
||||||
|
asyncTask.cancel(true);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
||||||
final JournalManager.Member member = (JournalManager.Member) getListAdapter().getItem(position);
|
final JournalManager.Member member = (JournalManager.Member) getListAdapter().getItem(position);
|
||||||
|
@ -166,6 +166,7 @@ public class JournalItemActivity extends BaseActivity implements Refreshable {
|
|||||||
public static class PrettyFragment extends Fragment {
|
public static class PrettyFragment extends Fragment {
|
||||||
CollectionInfo info;
|
CollectionInfo info;
|
||||||
SyncEntry syncEntry;
|
SyncEntry syncEntry;
|
||||||
|
private AsyncTask asyncTask;
|
||||||
|
|
||||||
public static PrettyFragment newInstance(CollectionInfo info, SyncEntry syncEntry) {
|
public static PrettyFragment newInstance(CollectionInfo info, SyncEntry syncEntry) {
|
||||||
PrettyFragment frag = new PrettyFragment();
|
PrettyFragment frag = new PrettyFragment();
|
||||||
@ -186,17 +187,24 @@ public class JournalItemActivity extends BaseActivity implements Refreshable {
|
|||||||
switch (info.type) {
|
switch (info.type) {
|
||||||
case ADDRESS_BOOK:
|
case ADDRESS_BOOK:
|
||||||
v = inflater.inflate(R.layout.contact_info, container, false);
|
v = inflater.inflate(R.layout.contact_info, container, false);
|
||||||
new LoadContactTask(v).execute();
|
asyncTask = new LoadContactTask(v).execute();
|
||||||
break;
|
break;
|
||||||
case CALENDAR:
|
case CALENDAR:
|
||||||
v = inflater.inflate(R.layout.event_info, container, false);
|
v = inflater.inflate(R.layout.event_info, container, false);
|
||||||
new LoadEventTask(v).execute();
|
asyncTask = new LoadEventTask(v).execute();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDestroyView() {
|
||||||
|
super.onDestroyView();
|
||||||
|
if (asyncTask != null)
|
||||||
|
asyncTask.cancel(true);
|
||||||
|
}
|
||||||
|
|
||||||
private class LoadEventTask extends AsyncTask<Void, Void, Event> {
|
private class LoadEventTask extends AsyncTask<Void, Void, Event> {
|
||||||
View view;
|
View view;
|
||||||
LoadEventTask(View v) {
|
LoadEventTask(View v) {
|
||||||
|
@ -41,6 +41,7 @@ public class ListEntriesFragment extends ListFragment implements AdapterView.OnI
|
|||||||
private EntityDataStore<Persistable> data;
|
private EntityDataStore<Persistable> data;
|
||||||
private CollectionInfo info;
|
private CollectionInfo info;
|
||||||
private JournalEntity journalEntity;
|
private JournalEntity journalEntity;
|
||||||
|
private AsyncTask asyncTask;
|
||||||
|
|
||||||
private TextView emptyTextView;
|
private TextView emptyTextView;
|
||||||
|
|
||||||
@ -74,11 +75,18 @@ public class ListEntriesFragment extends ListFragment implements AdapterView.OnI
|
|||||||
public void onViewCreated(View view, Bundle savedInstanceState) {
|
public void onViewCreated(View view, Bundle savedInstanceState) {
|
||||||
super.onViewCreated(view, savedInstanceState);
|
super.onViewCreated(view, savedInstanceState);
|
||||||
|
|
||||||
new JournalFetch().execute();
|
asyncTask = new JournalFetch().execute();
|
||||||
|
|
||||||
getListView().setOnItemClickListener(this);
|
getListView().setOnItemClickListener(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDestroyView() {
|
||||||
|
super.onDestroyView();
|
||||||
|
if (asyncTask != null)
|
||||||
|
asyncTask.cancel(true);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
||||||
EntryEntity entry = (EntryEntity) getListAdapter().getItem(position);
|
EntryEntity entry = (EntryEntity) getListAdapter().getItem(position);
|
||||||
|
Loading…
Reference in New Issue
Block a user