Android导航图式变化
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以添加 NavController.OnDestinationChangedListener 并根据当前片段设置方向。
将其添加到您的 Activity 的
onCreate
中:You can add
NavController.OnDestinationChangedListener
and set orientation according to the current fragment.Add this in your activity's
onCreate
:以下步骤可能对您有用:
不要从
androidmanifest.xml
。在
将在片段生命周期更改上执行回调conderfagmentManager
上注册活动
nav> navHostFragment
,该fragment
可见,从上面的回调。Note
片段标签或实例类型可用于基于App的导航设计,在生命周期回调中编写有条件的语句。
不要忘记来自
activity.ondestroy()
欢呼
The following steps could be useful for you:
Don't lock screen orientation from
AndroidManifest.xml
.Register a listener inside
Activity
on thechildFragmentManager
of theNavHostFragment
, that will execute callbacks on fragment lifecycle changeFragment
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 ????