From 5daf8934ca3e792f816e45a7a98b4b4b9ff0ec06 Mon Sep 17 00:00:00 2001 From: Christian Sadilek Date: Thu, 7 Oct 2021 13:41:13 -0400 Subject: [PATCH] [fenix] Closes https://github.com/mozilla-mobile/fenix/issues/21759: Do not render home fragment when launching to tab Co-authored-by: Sebastian Kaspari --- .../java/org/mozilla/fenix/HomeActivity.kt | 29 ++++++++++++------- .../java/org/mozilla/fenix/StartupFragment.kt | 19 ++++++++++++ .../customtabs/ExternalAppBrowserActivity.kt | 4 +++ app/src/main/res/navigation/nav_graph.xml | 13 ++++++++- 4 files changed, 54 insertions(+), 11 deletions(-) create mode 100644 app/src/main/java/org/mozilla/fenix/StartupFragment.kt diff --git a/app/src/main/java/org/mozilla/fenix/HomeActivity.kt b/app/src/main/java/org/mozilla/fenix/HomeActivity.kt index 78a3ddc040..b19757dc6f 100644 --- a/app/src/main/java/org/mozilla/fenix/HomeActivity.kt +++ b/app/src/main/java/org/mozilla/fenix/HomeActivity.kt @@ -132,7 +132,7 @@ import java.lang.ref.WeakReference * - browser screen */ @OptIn(ExperimentalCoroutinesApi::class) -@SuppressWarnings("TooManyFunctions", "LargeClass", "LongParameterList") +@SuppressWarnings("TooManyFunctions", "LargeClass", "LongParameterList", "LongMethod") open class HomeActivity : LocaleAwareAppCompatActivity(), NavHostActivity { // DO NOT MOVE ANYTHING ABOVE THIS, GETTING INIT TIME IS CRITICAL // we need to store startup timestamp for warm startup. we cant directly store @@ -229,12 +229,18 @@ open class HomeActivity : LocaleAwareAppCompatActivity(), NavHostActivity { it.start() } - if (!shouldStartOnHome() && - shouldNavigateBrowserFragmentOnColdStart(savedInstanceState) - ) { + // Unless the activity is recreated, navigate to home first (without rendering it) + // to add it to the back stack. + if (savedInstanceState == null) { + navigateToHome() + } + + if (!shouldStartOnHome() && shouldNavigateToBrowserOnColdStart(savedInstanceState)) { navigateToBrowserOnColdStart() - } else if (FeatureFlags.showStartOnHomeSettings) { - components.analytics.metrics.track(Event.StartOnHomeEnterHomeScreen) + } else { + if (FeatureFlags.showStartOnHomeSettings) { + components.analytics.metrics.track(Event.StartOnHomeEnterHomeScreen) + } } Performance.processIntentIfPerformanceTest(intent, this) @@ -879,13 +885,16 @@ open class HomeActivity : LocaleAwareAppCompatActivity(), NavHostActivity { open fun navigateToBrowserOnColdStart() { // Normal tabs + cold start -> Should go back to browser if we had any tabs open when we left last // except for PBM + Cold Start there won't be any tabs since they're evicted so we never will navigate - if (settings().shouldReturnToBrowser && - !browsingModeManager.mode.isPrivate - ) { + if (settings().shouldReturnToBrowser && !browsingModeManager.mode.isPrivate) { + // Navigate to home first (without rendering it) to add it to the back stack. openToBrowser(BrowserDirection.FromGlobal, null) } } + open fun navigateToHome() { + navHost.navController.navigate(NavGraphDirections.actionStartupHome()) + } + override fun attachBaseContext(base: Context) { base.components.strictMode.resetAfter(StrictMode.allowThreadDiskReads()) { super.attachBaseContext(base) @@ -1001,7 +1010,7 @@ open class HomeActivity : LocaleAwareAppCompatActivity(), NavHostActivity { @VisibleForTesting internal fun getSettings(): Settings = settings() - private fun shouldNavigateBrowserFragmentOnColdStart(savedInstanceState: Bundle?): Boolean { + private fun shouldNavigateToBrowserOnColdStart(savedInstanceState: Bundle?): Boolean { return isActivityColdStarted(intent, savedInstanceState) && !externalSourceIntentProcessors.any { it.process( diff --git a/app/src/main/java/org/mozilla/fenix/StartupFragment.kt b/app/src/main/java/org/mozilla/fenix/StartupFragment.kt new file mode 100644 index 0000000000..d12fc51af5 --- /dev/null +++ b/app/src/main/java/org/mozilla/fenix/StartupFragment.kt @@ -0,0 +1,19 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +package org.mozilla.fenix + +import androidx.fragment.app.Fragment +import org.mozilla.fenix.home.HomeFragment + +/** + * This empty fragment serves as a start destination in our navigation + * graph. It contains no layout and is fast to create compared to our + * [HomeFragment], which would otherwise be the start destination. + * + * When our [HomeActivity] is created we make a decision which fragment + * to navigate to, which makes sure we only render the [HomeFragment] + * as needed. + */ +class StartupFragment : Fragment() diff --git a/app/src/main/java/org/mozilla/fenix/customtabs/ExternalAppBrowserActivity.kt b/app/src/main/java/org/mozilla/fenix/customtabs/ExternalAppBrowserActivity.kt index 0603c0a6c6..566f00d7a6 100644 --- a/app/src/main/java/org/mozilla/fenix/customtabs/ExternalAppBrowserActivity.kt +++ b/app/src/main/java/org/mozilla/fenix/customtabs/ExternalAppBrowserActivity.kt @@ -53,6 +53,10 @@ open class ExternalAppBrowserActivity : HomeActivity() { // No-op for external app } + override fun navigateToHome() { + // No-op for external app + } + override fun handleNewIntent(intent: Intent) { // No-op for external app } diff --git a/app/src/main/res/navigation/nav_graph.xml b/app/src/main/res/navigation/nav_graph.xml index 6d9b80bb8f..821bdb8735 100644 --- a/app/src/main/res/navigation/nav_graph.xml +++ b/app/src/main/res/navigation/nav_graph.xml @@ -3,7 +3,13 @@ xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/nav_graph" - app:startDestination="@id/homeFragment"> + app:startDestination="@id/startupFragment"> + + + + +