在多模块应用中使用navigation不知不觉都是这么用

https://www.youtube.com/watch?v=lv1raAvwcgI&t=318s

省流总结:

假设我们有主模块 app, 功能模块 featureA, featureB,

featureA中有 screen1, screen2

featureB中有 screen3, screen4

核心common模块定义了Route1->screen1,Route2->screen2,Route3->screen3.....

如果在screen3 中要跳转到A中的screen1

有时候代码会是这样

@Composable
fun Screen3(
    navController: NavController,
    modifier: Modifier = Modifier
) {
    Box(
        modifier = Modifier
            .fillMaxSize(),
        contentAlignment = Alignment.Center
    ) {
        Button(
                onClick = {navController.navigate(Route.Screen1)}    
        ) {
            Text("Go to Screen 1")
        }
    }
}

我们把导航逻辑写到模块中带来的问题是,

如果我现在单独使用 featureB,但是featureB中因为导航的这个逻辑,又要不得不去关联featureA。

所以,需要把导航的逻辑到需要通过lambda来提升到外部去

@Composable
fun ScreenA(
    onButtonClick: () -> Unit,
    modifier: Modifier = Modifier
) {
    Box(
        modifier = Modifier
            .fillMaxSize(),
        contentAlignment = Alignment.Center
    ) {
        Button(
            onClick = onButtonClick //抛出导航逻辑
        ) {
            Text("Go to Screen 1")
        }
    }
}

所有的逻辑写在应用nav模块中

 NavigationMultiModulePrepTheme {
                Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
                    val navController = rememberNavController()
                    NavHost(
                        navController = navController,
                        startDestination = Route.ScreenA
                    ) {
                        composable<Route.Screen1> {
                            Screen1(
                                onButtonClick = {
                                    navController.navigate(Route.Screen3)
                                }
                            )
                        }
                        composable<Route.Screen2> {
                            Screen2(
                                onButtonClick = {
                                    navController.navigate(Route.Screen3)
                                }
                            )
                        }
                        composable<Route.Screen3> {
                            Screen3(
                                onButtonClick = {
                                    navController.navigate(Route.Screen1) {
                                        popUpTo<Route.Screen3> {
                                            inclusive = true
                                        }
                                    }
                                }
                            )
                        }
                    }
                }
            }

所以,我们不该在内部的功能部分看到navigate的导航逻辑

再进一步的话,可以把点击事件,转换成 事件 发送出去,再总的导航模块来处理具体的 导航事件

参考,更早的导航说明

关于compose中的navigation-优快云博客

A powerful library that manage Fragment for Android!为"单Activity + 多Fragment","多模块Activity 多Fragment"架构而生,简化开发,轻松解决动画、嵌套、事务相关等问题。为了更好的使用和了解该库,推荐阅读下面的文章:Fragment全解析系列(一):那些年踩过的坑Fragment全解析系列(二):正确的使用姿势Demo演示:均为单Activity 多Fragment,第一个为简单流式demo,第二个为仿微信交互的demo(全页面支持滑动退出),第三个为仿知乎交互的复杂嵌套demo下载APK   特性1、可以快速开发出各种嵌套设计的Fragment App2、悬浮球/摇一摇实时查看Fragment的栈视图Dialog,降低开发难度3、增加启动模式、startForResult等类似Activity方法4、类似Android事件分发机制的Fragment回退方法:onBackPressedSupport(),轻松为每个Fragment实现Back按键事件5、提供onSupportVisible()等生命周期方法,简化嵌套Fragment的开发过程; 提供统一的onLazyInitView()懒加载方法6、提供 Fragment转场动画 系列解决方案,动态更换动画7、提供Activity作用域的EventBus辅助类,Fragment通信更简单、独立(需要使用EventBusActivityScope库)8、支持SwipeBack滑动边缘退出(需要使用Fragmentation_SwipeBack库)      如何使用1. 项目下app的build.gradle中依赖:// appcompat-v7包是必须的,v1.1.9兼容v4-27.0.0 compile 'me.yokeyword:fragmentation:1.1.9' // 如果不想继承SupportActivity/Fragment,自己定制Support,可仅依赖: // compile 'me.yokeyword:fragmentation-core:1.1.9' // 如果想使用SwipeBack 滑动边缘退出Fragment/Activity功能,完整的添加规则如下: compile 'me.yokeyword:fragmentation:1.1.9' // swipeback基于fragmentation, 如果是自定制SupportActivity/Fragment,则参照SwipeBackActivity/Fragment实现即可 compile 'me.yokeyword:fragmentation-swipeback:1.1.9' // Activity作用域的EventBus,更安全,可有效避免after onSavenInstanceState()异常 compile 'me.yokeyword:eventbus-activity-scope:1.1.0' // Your EventBus's version compile 'org.greenrobot:eventbus:{version}'2. Activity继承SupportActivity:// v1.0.0开始,不强制继承SupportActivity,可使用接口+委托形式来实现自己的SupportActivity public class MainActivity extends SupportActivity {     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(...);         // 建议在Application里初始化         Fragmentation.builder()              // 显示悬浮球 ; 其他Mode:SHAKE: 摇一摇唤出   NONE:隐藏              .stackViewMode(Fragmentation.BUBBLE)              .debug(BuildConfig.DEBUG)              ... // 更多查看wiki或demo              .install();         if (findFragment(HomeFragment.class) == null)
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值