-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
seiko
committed
Jul 20, 2021
1 parent
84bf0c5
commit f7c980e
Showing
13 changed files
with
161 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
library/src/main/kotlin/com/github/fragivity/ActionPushTo.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.github.fragivity | ||
|
||
import androidx.core.os.bundleOf | ||
import androidx.fragment.app.FragivityFragmentNavigator.Companion.KEY_PUSH_TO | ||
import androidx.fragment.app.Fragment | ||
import androidx.navigation.clearBackStackEntry | ||
import kotlin.reflect.KClass | ||
|
||
/** | ||
* 跳转到目标页面,并清空所有旧页面 | ||
*/ | ||
@JvmSynthetic | ||
fun FragivityNavHost.pushTo(clazz: KClass<out Fragment>) { | ||
|
||
val node = getOrCreateNode(clazz) | ||
|
||
// 删除原有parent(NavGraph) | ||
node.parent?.remove(node) | ||
|
||
// 清空所有node | ||
viewModel.clearNodes() | ||
|
||
// 重新保存 | ||
addNode(node) | ||
|
||
// 主动清空旧NavHost防止内存泄漏 | ||
val viewModel = viewModel | ||
val navController = navController | ||
onCleared() | ||
|
||
// 修改开始id用于重建 | ||
viewModel.startNodeId = node.id | ||
|
||
with(navController) { | ||
// 清空返回栈 | ||
clearBackStackEntry() | ||
|
||
// 重建graph | ||
setGraph(createGraph(node), bundleOf( | ||
KEY_PUSH_TO to true | ||
)) | ||
|
||
// 重新绑定nav范围的viewModel | ||
fragivityHostViewModel.setUpNavHost(viewModel, this) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters