Android 导航组件活动意图标志 [英] Android Navigation Component Activity Intent Flags

查看:29
本文介绍了Android 导航组件活动意图标志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了从片段到活动的导航操作,但是我无法清除返回堆栈.当我执行从片段到新活动的导航操作并按下后退按钮时,我将返回到上一个活动和上一个片段.我无法使用导航图设置 Intent 标志来清除返回堆栈中的先前活动.

I have created a navigation action from a fragment to an activity, but I have no way of clearing the back stack. When I execute the navigation action from my fragment to my new activity, and I press the back button, I am taken back to the previous activity and previous fragment. I have no way of setting Intent flags, using the navigation graph, to clear the previous activity from the back stack.

<fragment
    android:id="@+id/loginFragment"
    android:name="com.myapp.auth.LoginFragment"
    android:label="login_fragment"
    tools:layout="@layout/login_fragment" >
    <action
        android:id="@+id/action_loginFragment_to_webActivity"
        app:destination="@id/webActivity"
        app:popUpTo="@id/loginFragment"
        app:popUpToInclusive="true" />
</fragment>
<activity
    android:id="@+id/webActivity"
    android:name="com.myapp.web.WebActivity"
    android:label="activity_web"
    tools:layout="@layout/activity_web" >
</activity>

PopToInclusive 标志在从片段导航到新活动时对后退按钮没有影响,即使它们可以在图表编辑器中设置.我可以使用后退按钮导航到堆栈中不再需要的上一个活动.

PopTo and Inclusive flags have no effect on the back button when navigating from a fragment to a new activity, even though they can be set in the graph editor. I am able to navigate, using the back button, to the previous activity that I no longer want in the stack.

在迁移到导航图之前,我可以使用 Intent 标志指定此行为:

Before migrating to the navigation graph, I could just specify this behavior with Intent flags:

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

如何使用导航图实现相同的功能?

How can I achieve the same thing with the navigation graph?

推荐答案

我不得不解决同样的问题.要解决这个问题,您要做的第一件事就是创建一个操作来导航到 Activity,就像您已经完成的那样.

I had to hack my way through the same problem. To solve this, the first thing you have to do is to create an action to navigate to the Activity, as you already had done.

例如:

<action
    android:id="@+id/action_frag_to_myActivity"
    app:destination="@id/myActivity"
    app:popUpTo="@id/myActivity" />

现在,您可以将参数作为 Intent extra 传递给 Activity,这样您就可以利用这一点让目标 Activity 完成脏活"并为您清除后台堆栈.

Now, you can pass arguments to the Activity as intent extras, so you can take advantage of that to make the destination Activity do the "dirty work" and clear the back stack for you.

假设您的导航图中有这个 Activity 标签:

Say that you have this Activity tag inside your navigation graph:

<activity
    android:id="@+id/myActivity"
    android:name="com.dummy.MyActivity"
    android:label="activity_my" />

您可以在其中添加一个参数并添加一个默认值.例如:

You could add an argument in it and add a default value. For example:

 <activity
    android:id="@+id/myActivity"
    android:name="com.dummy.MyActivity"
    android:label="activity_my">

        <argument
            android:name="clearBackstack"
            app:argType="boolean"
            android:defaultValue="true" />

</activity>

然后一旦你调用 findNavController().navigate(R.id.myActivity) 它将传递一个带有键 "clearBackstack" 的意图额外,你可以阅读在 Activity onCreate() 方法中.类似于下面的示例.

Then once you call findNavController().navigate(R.id.myActivity) it'll pass an intent extra with the key "clearBackstack" which you can read inside the Activity onCreate() method. Something like the example below.

MyActivity.kt

private val EXTRA_LOGOUT = "clearBackstack"

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    if (intent.extras?.getBoolean(EXTRA_LOGOUT) == true) {
        clearBackstack()
    } else {
        setContentView(R.layout.activity_my)
    }
}

private fun clearBackstack() {
    startActivity(Intent(this, MyActivity::class.java).apply {
        addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
    })

    finish()
}

请记住,您可以修改参数并自定义您想要在目标活动上执行的操作.您还可以在导航到该值后修改该值.您可以在此处的文档中阅读更多相关信息.

Keep in mind you can tinker around with the arguments and customize what you want to do on the destination Activity. You could also modify the value once you navigate to it. You can read more about it here in the docs.

这篇关于Android 导航组件活动意图标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆