Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to set fragment max lifecycle #2

Merged
merged 3 commits into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ android {

compileSdk rootProject.ext.compileSdkVersion

lintOptions {
abortOnError false
}
defaultConfig {
applicationId "com.ncapdevi.sample"
minSdkVersion rootProject.ext.minSdkVersion
Expand All @@ -27,6 +24,9 @@ android {
targetCompatibility = JavaVersion.VERSION_17
sourceCompatibility = JavaVersion.VERSION_17
}
lint {
abortOnError false
}
}

repositories {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
buildscript {
ext.kotlin_version = '1.9.10'
ext.kotlin_version = '1.9.20'
ext.spek_version = '2.0.19'
repositories {
google()
mavenCentral()

}
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"
Expand Down
10 changes: 5 additions & 5 deletions frag-nav/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,13 @@ 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'

compileSdk rootProject.ext.compileSdkVersion

lintOptions {
abortOnError false
}

defaultConfig {
minSdkVersion rootProject.ext.minSdkVersion
Expand Down Expand Up @@ -59,6 +56,9 @@ android {
}
}
}
lint {
abortOnError false
}
}

dependencies {
Expand Down Expand Up @@ -93,7 +93,7 @@ afterEvaluate {
from components.release
groupId = 'com.github.hannta'
artifactId = 'frag-nav'
version = '3.4.0'
version = '3.5.0'
}
}
}
Expand Down
44 changes: 27 additions & 17 deletions frag-nav/src/main/java/com/ncapdevi/fragnav/FragNavController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -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