From d276f826350690b852efb2e4df148114410f779c Mon Sep 17 00:00:00 2001 From: Hannu Tapanila Date: Sun, 17 Mar 2024 16:37:21 +0200 Subject: [PATCH 1/3] Update gradle & kotlin versions --- app/build.gradle | 6 +++--- build.gradle | 4 ++-- frag-nav/build.gradle | 6 +++--- gradle/wrapper/gradle-wrapper.properties | 2 +- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index e12ba09..fdf0c4d 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -6,9 +6,6 @@ android { compileSdk rootProject.ext.compileSdkVersion - lintOptions { - abortOnError false - } defaultConfig { applicationId "com.ncapdevi.sample" minSdkVersion rootProject.ext.minSdkVersion @@ -27,6 +24,9 @@ android { targetCompatibility = JavaVersion.VERSION_17 sourceCompatibility = JavaVersion.VERSION_17 } + lint { + abortOnError false + } } repositories { diff --git a/build.gradle b/build.gradle index cf250e0..50ceea9 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,5 @@ buildscript { - ext.kotlin_version = '1.9.10' + ext.kotlin_version = '1.9.20' ext.spek_version = '2.0.19' repositories { google() @@ -7,7 +7,7 @@ buildscript { } dependencies { - classpath 'com.android.tools.build:gradle:8.2.2' + classpath 'com.android.tools.build:gradle:8.3.0' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath "de.mannodermaus.gradle.plugins:android-junit5:1.8.2.0" classpath "org.jacoco:org.jacoco.core:0.8.7" diff --git a/frag-nav/build.gradle b/frag-nav/build.gradle index 7e597f8..68344e4 100644 --- a/frag-nav/build.gradle +++ b/frag-nav/build.gradle @@ -17,9 +17,6 @@ android { compileSdk rootProject.ext.compileSdkVersion - lintOptions { - abortOnError false - } defaultConfig { minSdkVersion rootProject.ext.minSdkVersion @@ -59,6 +56,9 @@ android { } } } + lint { + abortOnError false + } } dependencies { diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 15de902..e411586 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists From a950c581f730dcc1ef6ee4d9f53f7f3d849f4fbe Mon Sep 17 00:00:00 2001 From: Hannu Tapanila Date: Sun, 17 Mar 2024 16:58:19 +0200 Subject: [PATCH 2/3] Set fragment max lifecylce --- .../sample/activities/BottomTabsActivity.kt | 1 + .../com/ncapdevi/fragnav/FragNavController.kt | 44 ++++++++++++------- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/com/ncapdevi/sample/activities/BottomTabsActivity.kt b/app/src/main/java/com/ncapdevi/sample/activities/BottomTabsActivity.kt index 5ccad6a..8e50627 100644 --- a/app/src/main/java/com/ncapdevi/sample/activities/BottomTabsActivity.kt +++ b/app/src/main/java/com/ncapdevi/sample/activities/BottomTabsActivity.kt @@ -32,6 +32,7 @@ class BottomTabsActivity : AppCompatActivity(), BaseFragment.FragmentNavigation, transactionListener = this@BottomTabsActivity rootFragmentListener = this@BottomTabsActivity createEager = true + setMaxLifecycleOnSwitch = true fragNavLogger = object : FragNavLogger { override fun error(message: String, throwable: Throwable) { Log.e(TAG, message, throwable) diff --git a/frag-nav/src/main/java/com/ncapdevi/fragnav/FragNavController.kt b/frag-nav/src/main/java/com/ncapdevi/fragnav/FragNavController.kt index 5caafc7..a8e226b 100644 --- a/frag-nav/src/main/java/com/ncapdevi/fragnav/FragNavController.kt +++ b/frag-nav/src/main/java/com/ncapdevi/fragnav/FragNavController.kt @@ -11,6 +11,7 @@ import androidx.fragment.app.DialogFragment import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentManager import androidx.fragment.app.FragmentTransaction +import androidx.lifecycle.Lifecycle import com.ncapdevi.fragnav.tabhistory.* import org.json.JSONArray import java.lang.ref.WeakReference @@ -65,6 +66,12 @@ class FragNavController constructor(private val fragmentManger: FragmentManager, var fragmentHideStrategy = FragNavController.DETACH var createEager = false + /** + * Set this flag to true will set the maximum lifecycle state during the transaction, + * so the lifecycle will be triggered properly. + */ + var setMaxLifecycleOnSwitch = false + @TabIndex @get:CheckResult @get:TabIndex @@ -219,7 +226,12 @@ class FragNavController constructor(private val fragmentManger: FragmentManager, when { shouldDetachAttachOnSwitch() -> ft.detach(fragment) shouldRemoveAttachOnSwitch() -> ft.remove(fragment) - else -> ft.hide(fragment) + else -> { + if (setMaxLifecycleOnSwitch) { + ft.setMaxLifecycle(fragment, Lifecycle.State.STARTED) + } + ft.hide(fragment) + } } } else { mCurrentFrag = fragment @@ -273,8 +285,18 @@ class FragNavController constructor(private val fragmentManger: FragmentManager, if (index == NO_TAB) { commitTransaction(ft, transactionOptions) } else { + val shouldAttach = shouldDetachAttachOnSwitch() || shouldRemoveAttachOnSwitch() + //Attempt to reattach previous fragment - fragment = addPreviousFragment(ft, shouldDetachAttachOnSwitch() || shouldRemoveAttachOnSwitch()) + fragment = addPreviousFragment(ft, shouldAttach) + + if (!shouldAttach && setMaxLifecycleOnSwitch) { + fragmentCache.values.forEach { ref -> + val frag = ref.get() ?: return@forEach + ft.setMaxLifecycle(frag, Lifecycle.State.STARTED) + } + ft.setMaxLifecycle(fragment, Lifecycle.State.RESUMED) + } commitTransaction(ft, transactionOptions) } mCurrentFrag = fragment @@ -665,19 +687,10 @@ class FragNavController constructor(private val fragmentManger: FragmentManager, } } - setTransitionStyle(options.transitionStyle) - setTransition(options.transition) - options.sharedElements.forEach { sharedElement -> - sharedElement.first?.let { - sharedElement.second?.let { it1 -> - addSharedElement( - it, - it1 - ) - } - } + options.sharedElements.forEach { (element, elementName) -> + addSharedElement(element, elementName) } when { @@ -764,10 +777,7 @@ class FragNavController constructor(private val fragmentManger: FragmentManager, * * @param outState The Bundle to save state information to */ - fun onSaveInstanceState(outState: Bundle?) { - if (outState == null) { - return - } + fun onSaveInstanceState(outState: Bundle) { // Write tag count outState.putInt(EXTRA_TAG_COUNT, tagCount) From bea75a2f7884c9bdb0ad2eee7cd9d86391b0024e Mon Sep 17 00:00:00 2001 From: Hannu Tapanila Date: Sun, 17 Mar 2024 16:59:05 +0200 Subject: [PATCH 3/3] Version bump 3.5.0 --- frag-nav/build.gradle | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frag-nav/build.gradle b/frag-nav/build.gradle index 68344e4..86a5bf8 100644 --- a/frag-nav/build.gradle +++ b/frag-nav/build.gradle @@ -10,7 +10,7 @@ apply plugin: 'jacoco' apply plugin: "de.mannodermaus.android-junit5" group = "com.ncapdevi" -version = "3.4.0" +version = "3.5.0" android { namespace = 'com.ncapdevi.fragnav' @@ -93,7 +93,7 @@ afterEvaluate { from components.release groupId = 'com.github.hannta' artifactId = 'frag-nav' - version = '3.4.0' + version = '3.5.0' } } }