Android导航图式变化

发布于 2025-01-20 18:35:39 字数 111 浏览 0 评论 0原文

我正在尝试使用 Android 导航组件遵循单一活动模式,我的 %99 片段是纵向的,但我需要制作一个新的片段可以是纵向或横向的,而不添加新的活动,我该如何实现。我找不到任何资源。是否可以 ?如果是怎么回事?

I am trying to follow single activity pattern with android navigation component and my %99 of fragment are portrait but I need to make a new fragment can be portrait or landscape without adding new activity how can I achieve. I could't find any resource. is it possible ? if it is how ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雅心素梦 2025-01-27 18:35:39

您可以添加 NavController.OnDestinationChangedListener 并根据当前片段设置方向。

将其添加到您的 Activity 的 onCreate 中:

val navHostFragment = supportFragmentManager.findFragmentById(R.id.your_nav_host_fragment) as NavHostFragment
navHostFragment.navController..addOnDestinationChangedListener { _, destination, _ ->
    if (destination.id == R.id.destination_with_orientation) {
        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR
    } else {
        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
    }
}

You can add NavController.OnDestinationChangedListener and set orientation according to the current fragment.

Add this in your activity's onCreate:

val navHostFragment = supportFragmentManager.findFragmentById(R.id.your_nav_host_fragment) as NavHostFragment
navHostFragment.navController..addOnDestinationChangedListener { _, destination, _ ->
    if (destination.id == R.id.destination_with_orientation) {
        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR
    } else {
        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
    }
}
奢欲 2025-01-27 18:35:39

以下步骤可能对您有用:

  1. 不要从androidmanifest.xml

  2. conderfagmentManager上注册活动 nav> navHostFragment,该将在片段生命周期更改上执行回调

override fun onCreate(savedInstanceState: Bundle?) {
   //...
   
   val fragments = supportFragmentManager.fragments
   check(fragments.size == 1) {
       val baseMessage = "Expected 1 fragment to host the app's navigation. Instead found ${fragments.size}."
       if (fragments.size == 0) {
           val suggestion = "(Make sure you specify `android:name=\"androidx.navigation.fragment." +
                   "NavHostFragment\"` or override setUpNavigationGraph)"
           "$baseMessage $suggestion"

       } else baseMessage
   }

   with(fragments[0].childFragmentManager) {
       registerFragmentLifecycleCallbacks(CustomFragmentLifecycleCallbacks(), false)
   }
}

private inner class CustomFragmentLifecycleCallbacks : FragmentManager.FragmentLifecycleCallbacks() {
   override fun onFragmentViewCreated(fm: FragmentManager, f: Fragment, v: View, savedInstanceState: Bundle?) {}
   override fun onFragmentViewDestroyed(fm: FragmentManager, f: Fragment) {}
}
  1. 。 // riptutorial.com/android/example/21077/lock-screen-s-rotation-programantically上“ rel =” nofollow noreferrer'>指南锁定/解锁屏幕方向取决于fragment可见,从上面的回调。

Note

片段标签或实例类型可用于基于App的导航设计,在生命周期回调中编写有条件的语句。

不要忘记来自activity.ondestroy()


欢呼

The following steps could be useful for you:

  1. Don't lock screen orientation from AndroidManifest.xml.

  2. Register a listener inside Activity on the childFragmentManager of the NavHostFragment, that will execute callbacks on fragment lifecycle change

override fun onCreate(savedInstanceState: Bundle?) {
   //...
   
   val fragments = supportFragmentManager.fragments
   check(fragments.size == 1) {
       val baseMessage = "Expected 1 fragment to host the app's navigation. Instead found ${fragments.size}."
       if (fragments.size == 0) {
           val suggestion = "(Make sure you specify `android:name=\"androidx.navigation.fragment." +
                   "NavHostFragment\"` or override setUpNavigationGraph)"
           "$baseMessage $suggestion"

       } else baseMessage
   }

   with(fragments[0].childFragmentManager) {
       registerFragmentLifecycleCallbacks(CustomFragmentLifecycleCallbacks(), false)
   }
}

private inner class CustomFragmentLifecycleCallbacks : FragmentManager.FragmentLifecycleCallbacks() {
   override fun onFragmentViewCreated(fm: FragmentManager, f: Fragment, v: View, savedInstanceState: Bundle?) {}
   override fun onFragmentViewDestroyed(fm: FragmentManager, f: Fragment) {}
}
  1. Follow this guide to lock/unlock screen orientation depending upon which Fragment is visible, from the above callback.

NOTE

Fragment tags or instance type could be used for writing conditional statements inside the lifecycle callbacks, based on app's navigation design.

Don't forget to unregisterFragmentLifecycleCallbacks from Activity.onDestroy()


Cheers ????

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文