From ba2f91190948375adf37855cdbd778a86a0c1c73 Mon Sep 17 00:00:00 2001 From: Joshua Soberg Date: Wed, 3 Feb 2021 22:03:23 -0500 Subject: [PATCH] #635 - Make sure that we're unregistering lifecycle observers to avoid memory leaks --- .../Activities/AuthenticateActivity.java | 25 +++++++++++++------ 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/org/shadowice/flocke/andotp/Activities/AuthenticateActivity.java b/app/src/main/java/org/shadowice/flocke/andotp/Activities/AuthenticateActivity.java index 0c1a975b..4de8be91 100644 --- a/app/src/main/java/org/shadowice/flocke/andotp/Activities/AuthenticateActivity.java +++ b/app/src/main/java/org/shadowice/flocke/andotp/Activities/AuthenticateActivity.java @@ -66,6 +66,7 @@ public class AuthenticateActivity extends BaseActivity private String newEncryption = ""; private String existingAuthCredentials; private boolean isAuthUpgrade = false; + private ProcessLifecycleObserver observer; private TextInputLayout passwordLayout; private TextInputEditText passwordInput; @@ -109,8 +110,9 @@ public class AuthenticateActivity extends BaseActivity } }); + observer = new ProcessLifecycleObserver(); ProcessLifecycleOwner.get().getLifecycle() - .addObserver(new ProcessLifecycleObserver()); + .addObserver(observer); getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); } @@ -174,6 +176,15 @@ public class AuthenticateActivity extends BaseActivity setupUiForTaskState(false); } + private class ProcessLifecycleObserver implements DefaultLifecycleObserver { + @Override + public void onStop(LifecycleOwner owner) { + if (settings.getRelockOnBackground()) { + cancelBackgroundTask(); + } + } + } + @Override public void onResume() { super.onResume(); @@ -201,13 +212,11 @@ public class AuthenticateActivity extends BaseActivity unlockProgress.setVisibility(isTaskRunning ? View.VISIBLE : View.GONE); } - private class ProcessLifecycleObserver implements DefaultLifecycleObserver { - @Override - public void onStop(LifecycleOwner owner) { - if (settings.getRelockOnBackground()) { - cancelBackgroundTask(); - } - } + @Override + protected void onDestroy() { + super.onDestroy(); + ProcessLifecycleOwner.get().getLifecycle() + .removeObserver(observer); } @Override