如何使用导航组件从一个活动导航到另一个活动,并从堆栈中删除第一个活动?

如何使用导航组件从一个活动导航到另一个活动,并从堆栈中删除第一个活动?

问题描述:

在我的应用程序中,我有两个活动(AuthenticationActivity和MainActivity),每个活动都有一个导航图和大量片段.我创建了一个动作,可以从AuthenticationActivity的图形片段导航到MainActivity,但是即使将"popTo"设置为Authentication graph id,它也不会从堆栈中删除AuthenticationActivity.

In my app I have two activities (AuthenticationActivity and MainActivity), each one with a nav graph and a big flow of fragments. I've created an action to navigate from a fragment of the AuthenticationActivity's graph to the MainActivity but it does not remove the AuthenticationActivity from the stack even if I set 'popTo' to the Authentication graph id.

在导航方法之后完成身份验证活动:

finish authentication activity after navigate method:

button2.setOnClickListener {
        view.findNavController().navigate(R.id.action_addItemFragment_to_mainActivity)
        (activity as AuthenticationActivity).finish()
    }