From 0fc2fe2faf2c02b5db7c47053a1d8c17c9521a7d Mon Sep 17 00:00:00 2001 From: Ammar Githam Date: Sat, 22 Aug 2020 15:45:54 +0900 Subject: [PATCH] Stop notification checker when app in background --- .../java/awais/instagrabber/activities/Main.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/activities/Main.java b/app/src/main/java/awais/instagrabber/activities/Main.java index 283a3f1c..21d5a5bb 100755 --- a/app/src/main/java/awais/instagrabber/activities/Main.java +++ b/app/src/main/java/awais/instagrabber/activities/Main.java @@ -70,6 +70,7 @@ import static awais.instagrabber.utils.Utils.notificationManager; import static awais.instagrabber.utils.Utils.settingsHelper; public final class Main extends BaseLanguageActivity { + private static final int INITIAL_DELAY_MILLIS = 200; public static FetchListener scanHack; public static ItemGetter itemGetter; // -------- items -------- @@ -117,6 +118,7 @@ public final class Main extends BaseLanguageActivity { private Stack queriesStack; private DataBox.CookieModel cookieModel; private Runnable runnable; + private Handler handler; @Override protected void onCreate(@Nullable final Bundle bundle) { @@ -257,7 +259,7 @@ public final class Main extends BaseLanguageActivity { mainHelper.onIntent(getIntent()); - final Handler handler = new Handler(); + handler = new Handler(); runnable = () -> { final GetActivityAsyncTask activityAsyncTask = new GetActivityAsyncTask(uid, cookie, result -> { if (result == null) { @@ -305,7 +307,7 @@ public final class Main extends BaseLanguageActivity { activityAsyncTask.execute(); handler.postDelayed(runnable, 60000); }; - handler.postDelayed(runnable, 200); + handler.postDelayed(runnable, INITIAL_DELAY_MILLIS); } private void downloadSelectedItems() { @@ -559,12 +561,18 @@ public final class Main extends BaseLanguageActivity { @Override protected void onPause() { if (mainHelper != null) mainHelper.onPause(); + if (handler != null && runnable != null) { + handler.removeCallbacks(runnable); + } super.onPause(); } @Override protected void onResume() { if (mainHelper != null) mainHelper.onResume(); + if (handler != null && runnable != null) { + handler.postDelayed(runnable, INITIAL_DELAY_MILLIS); + } super.onResume(); }