From 7ea2ae853a55ba3f087a5f38815973af03f171f8 Mon Sep 17 00:00:00 2001 From: Ammar Githam Date: Mon, 21 Sep 2020 03:45:58 +0900 Subject: [PATCH] Fix Android Q leak. Reported by LeakCanary, more details here: https://issuetracker.google.com/issues/139738913 --- .../instagrabber/activities/MainActivity.java | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/activities/MainActivity.java b/app/src/main/java/awais/instagrabber/activities/MainActivity.java index a5106baf..d66637cc 100644 --- a/app/src/main/java/awais/instagrabber/activities/MainActivity.java +++ b/app/src/main/java/awais/instagrabber/activities/MainActivity.java @@ -29,6 +29,7 @@ import androidx.appcompat.widget.SearchView; import androidx.appcompat.widget.Toolbar; import androidx.coordinatorlayout.widget.CoordinatorLayout; import androidx.core.app.NotificationManagerCompat; +import androidx.fragment.app.FragmentManager; import androidx.lifecycle.LiveData; import androidx.navigation.NavBackStackEntry; import androidx.navigation.NavController; @@ -68,7 +69,7 @@ import awais.instagrabber.utils.TextUtils; import static awais.instagrabber.utils.NavigationExtensions.setupWithNavController; import static awais.instagrabber.utils.Utils.settingsHelper; -public class MainActivity extends BaseLanguageActivity { +public class MainActivity extends BaseLanguageActivity implements FragmentManager.OnBackStackChangedListener { private static final String TAG = "MainActivity"; private static final List SHOW_BOTTOM_VIEW_DESTINATIONS = Arrays.asList( @@ -93,7 +94,8 @@ public class MainActivity extends BaseLanguageActivity { R.id.directMessagesSettingsFragment, R.id.notificationsViewer, R.id.themePreferencesFragment, - R.id.favoritesFragment); + R.id.favoritesFragment, + R.id.backupPreferencesFragment); private static final Map NAV_TO_MENU_ID_MAP = new HashMap<>(); private static final List REMOVE_COLLAPSING_TOOLBAR_SCROLL_DESTINATIONS = Collections.singletonList(R.id.commentsViewerFragment); private static final String FIRST_FRAGMENT_GRAPH_INDEX_KEY = "firstFragmentGraphIndex"; @@ -108,6 +110,7 @@ public class MainActivity extends BaseLanguageActivity { private Handler suggestionsFetchHandler; private int firstFragmentGraphIndex; private boolean isActivityCheckerServiceBound = false; + private boolean isBackStackEmpty = false; private final ServiceConnection serviceConnection = new ServiceConnection() { @Override @@ -154,6 +157,7 @@ public class MainActivity extends BaseLanguageActivity { if (!TextUtils.isEmpty(cookie) && settingsHelper.getBoolean(Constants.CHECK_ACTIVITY)) { bindActivityCheckerService(); } + getSupportFragmentManager().addOnBackStackChangedListener(this); } @Override @@ -213,6 +217,21 @@ public class MainActivity extends BaseLanguageActivity { unbindActivityCheckerService(); } + @Override + public void onBackPressed() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && isTaskRoot() && isBackStackEmpty) { + finishAfterTransition(); + } else { + super.onBackPressed(); + } + } + + @Override + public void onBackStackChanged() { + final int backStackEntryCount = getSupportFragmentManager().getBackStackEntryCount(); + isBackStackEmpty = backStackEntryCount == 0; + } + private void createNotificationChannels() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { final NotificationManagerCompat notificationManager = NotificationManagerCompat.from(getApplicationContext());