gpt4 book ai didi

android - 仅具有上下文的导航架构组件导航

转载 作者:行者123 更新时间:2023-11-29 00:56:04 25 4
gpt4 key购买 nike

是否可以获取 NavigationController 并仅使用 Context 执行导航?现在看来没有。这将需要对我们的应用程序进行如此重大的重组,我觉得我一定遗漏了一些东西。

如果用户已注销,我们需要导航到 LoginFragment。此注销检查发生在几个不是 Activity 或 Fragment 的地方。例如,OkHttpClient 中的 Authenticator。

目前这工作正常,因为您可以从任何地方访问 ApplicationContext,这就是您创建 Intent、使用它启动 Activity 并在 backStack 顶部添加新 Activity 所需的全部。

最佳答案

根据 Implementing Navigation documentation :

Note: The Navigation components are designed for apps that have one main activity with multiple Fragment destinations. The main activity hosts the navigation graph and is responsible for swapping destinations as needed. In an app with multiple activity destinations, each additional activity hosts its own navigation graph.

因此,没有。没有全局 NavController存在于您的整个应用程序中 - 每个 Activity 都有自己的 NavController和相关的导航图。

但是,并不要求您一次性将整个应用全部切换到导航。根据 Migrate to Navigation documentation ,您可以一次转换一个 Activity,继续像以前一样针对此特定情况使用单独的登录 Activity。

这得益于对 <activity> 的内置支持目的地,当你navigate()时的目的地对他们来说,做一个startActivity()打电话,而不是 FragmentTransaction .如果您的 UI 中有一个“登录”按钮,您希望将其绑定(bind)到启动登录 Activity ,这将很有用。

虽然有一些很好的理由转向单一 Activity 模型,如 Single Activity: Why, When, and How talk 中所述。 , 但是 the end of the talk确实强调:

If you find yourself contorting your own experience and it's not making sense to you, don't do it. If something is working, that's good. Keep it working. But if you're finding you're running into issues, you're having inconsistent behavior, or you want to do things like share ViewModels, maybe that's the time to think about moving towards a single Activity structure.

关于android - 仅具有上下文的导航架构组件导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54660966/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com