我有两个活动,一个保存登录过程的所有片段,另一个保存主应用程序的所有片段.

Let's say I want to navigate from Activity1 (that holds all navigation graph of Login) to Activity2 (That holds all the navigation graph for the main app)

  class LoginActivity : AppCompatActivity() {
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_login)
        }
    
        fun goToMainActivity(){
            startActivity(Intent(this,MainActivity::class.java))
            finish()
        }
    }

这里我称之为goToMainActivity()方法

 class LoginFragment : Fragment() {
    
        override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
        ): View? {
            return inflater.inflate(R.layout.fragment_login,container,false)
        }
    
        override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            super.onViewCreated(view, savedInstanceState)
    
            btn_go.setOnClickListener {
                // call the method goToMainActivity() to kill all fragments contained by that Activity and move foward to MainActivity with another nav_graph
            }
        }
    }

Since LoginActivity holds a nav_graph and is the navigation host for all the Login Fragments, now I want to kill all the fragments contained to LoginActivity and move towards a new Activity (MainActivity) that holds a different nav graph

这是做这件事的好方法吗?或者我应该用不同的方式导航?

推荐答案

You don't need to define a second activity, simply add a second navigation graph to your nav_graph.xml file. Something like:

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools" android:id="@+id/nav_graph"
            app:startDestination="@id/loginFragment">

    <fragment
            android:id="@+id/loginFragment"
            android:name="com.mycompany.loginFragment"
            tools:layout="@layout/fragment_login"
            android:label="Login" >
         <action
                android:id="@+id/action_loginFragment_to_new_graph"
                app:destination="@id/new_graph" />
    </fragment>

    <include app:graph="@navigation/new_graph" />
</navigation>

Then, with your navController, navigate the action:

navController.navigate(R.id.action_loginFragment_to_new_graph)

Kotlin相关问答推荐

Compose:LaunchedEffect在密钥更改后不会重新启动

某些公共函数显然不能在类实例上访问;Klaxon示例

Spring Boot kotlin协程不能并行运行

合并状态流

Spring Boot Kotlin 数据类未使用 REST 控制器中的默认值初始化

在 kotlin 中使具体化字段可选

Kotlin 协程按顺序执行,但仅在生产机器上执行

为什么在 Kotlin 中调用私有构造函数会导致错误为无法访问 是什么?

Kotlin 条件格式字符串

使用 Hilt 注入 CoroutineWorker

retrofit 响应代码 405 并带有消息method not allowed here

使用 Kotlin 创建新目录,Mkdir() 不起作用

如何在使用 Gradle 的 AppEngine 项目中使用 Kotlin

RecyclerView SnapHelper无法显示第一个/最后一个元素

Jetpack Compose-居中文本

在android的默认浏览器 Select 列表中添加我的浏览器?

Android:Exoplayer - ExtractorMediaSource 已弃用

如何计算Kotlin中的百分比

@StringRes、@DrawableRes、@LayoutRes等android注释使用kotlin参数进行判断

Kotlin - 错误:Could not find or load main class _DefaultPackage