diff --git a/api/src/com/todoroo/andlib/utility/AndroidUtilities.java b/api/src/com/todoroo/andlib/utility/AndroidUtilities.java index 04c9f89f9..019086b05 100644 --- a/api/src/com/todoroo/andlib/utility/AndroidUtilities.java +++ b/api/src/com/todoroo/andlib/utility/AndroidUtilities.java @@ -21,6 +21,7 @@ import java.util.Map; import java.util.Map.Entry; import android.app.Activity; +import android.content.BroadcastReceiver; import android.content.ContentValues; import android.content.Context; import android.content.Intent; @@ -761,4 +762,18 @@ public class AndroidUtilities { return false; } + /** + * Wraps a call to Activity.unregisterReceiver in a try/catch block to prevent + * exceptions being thrown if receiver was never registered with that activity + * @param activity + * @param receiver + */ + public static void tryUnregisterReceiver(Activity activity, BroadcastReceiver receiver) { + try { + activity.unregisterReceiver(receiver); + } catch (IllegalArgumentException e) { + // Receiver wasn't registered for some reason + } + } + } diff --git a/astrid/src/com/todoroo/astrid/activity/AstridActivity.java b/astrid/src/com/todoroo/astrid/activity/AstridActivity.java index e8c9dcd2c..d047ba883 100644 --- a/astrid/src/com/todoroo/astrid/activity/AstridActivity.java +++ b/astrid/src/com/todoroo/astrid/activity/AstridActivity.java @@ -124,16 +124,8 @@ public class AstridActivity extends FragmentActivity @Override protected void onPause() { super.onPause(); - tryUnregisterReceiver(reminderReceiver); - tryUnregisterReceiver(repeatConfirmationReceiver); - } - - public void tryUnregisterReceiver(BroadcastReceiver receiver) { - try { - unregisterReceiver(receiver); - } catch (IllegalArgumentException e) { - // Receiver wasn't registered for some reason - } + AndroidUtilities.tryUnregisterReceiver(this, reminderReceiver); + AndroidUtilities.tryUnregisterReceiver(this, repeatConfirmationReceiver); } /** diff --git a/astrid/src/com/todoroo/astrid/activity/TaskListFragment.java b/astrid/src/com/todoroo/astrid/activity/TaskListFragment.java index 11f38d2f2..ceea48b32 100644 --- a/astrid/src/com/todoroo/astrid/activity/TaskListFragment.java +++ b/astrid/src/com/todoroo/astrid/activity/TaskListFragment.java @@ -611,13 +611,11 @@ public class TaskListFragment extends ListFragment implements OnScrollListener, public void onPause() { super.onPause(); StatisticsService.sessionPause(); - try { - getActivity().unregisterReceiver(detailReceiver); - getActivity().unregisterReceiver(refreshReceiver); - syncActionHelper.unregister(); - } catch (IllegalArgumentException e) { - // might not have fully initialized - } + + AndroidUtilities.tryUnregisterReceiver(getActivity(), detailReceiver); + AndroidUtilities.tryUnregisterReceiver(getActivity(), refreshReceiver); + syncActionHelper.unregister(); + backgroundTimer.cancel(); } diff --git a/astrid/src/com/todoroo/astrid/helper/SyncActionHelper.java b/astrid/src/com/todoroo/astrid/helper/SyncActionHelper.java index c73950519..f3a6796ff 100644 --- a/astrid/src/com/todoroo/astrid/helper/SyncActionHelper.java +++ b/astrid/src/com/todoroo/astrid/helper/SyncActionHelper.java @@ -27,6 +27,7 @@ import com.todoroo.andlib.service.Autowired; import com.todoroo.andlib.service.ContextManager; import com.todoroo.andlib.service.DependencyInjectionService; import com.todoroo.andlib.service.ExceptionService; +import com.todoroo.andlib.utility.AndroidUtilities; import com.todoroo.andlib.utility.DateUtilities; import com.todoroo.andlib.utility.Preferences; import com.todoroo.astrid.activity.TaskListFragment; @@ -128,7 +129,7 @@ public class SyncActionHelper { } public void unregister() { - activity.unregisterReceiver(syncActionReceiver); + AndroidUtilities.tryUnregisterReceiver(activity, syncActionReceiver); } public void request() {