android - fragment 是否应该能够更改主机 Activity 内的 View 状态?

标签 android android-fragments android-activity

如果我需要在某个 fragment 膨胀时在 Activity 中隐藏一个 View ,是否可以让 Fragment 进行 State Change?

例如,我有三个 Fragment(FragmentA、FragmentB、FragmentC)和一个 Activty。 Activity 有一个 BottomNavigation View,但是如果 FragmentB 在 Activity 中膨胀,它的可见性应该设置为 Gone。

如果我将 BottomNavigation Visibility 的管理放在 fragment 中,我确信每当该 fragment 膨胀时, View 肯定会设置为消失。

我唯一的问题是,如果有时间我需要重用该 fragment 并同时显示 BottomNavigation。我无法这样做,因为 Fragment 会自动将 BottomNavigation 的 Visibility 设置为 Gone。

谁能给我一些提示?提前致谢。

最佳答案

在您的情况下,不要控制 Fragment 内 BottomNavigation 的可见性,而是在 Activity 内使用 回调 进行控制。 read about callback this in part "Creating event callbacks to the activity"

Fragments 应该是自给自足的,不应该知道任何关于其他 Fragments 和 Activites 的信息。

关于android - fragment 是否应该能够更改主机 Activity 内的 View 状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51893408/

相关文章:

android - 防止 ViewPager 总是在位置 0 实例化 Fragment?

android - 如何在带有 fragment 的操作栏上使用按钮?

android - 来自标签的数据未正确传递到新 Activity

android - Android Activity 中的主循环

android - Proguard 和 XmlPullParser 的问题

c# - 如果文本不适合 Android 中的 Textview 小部件,是否切断文本?

android - android native 支持 RTP 和/或 SCTP 吗?

java - 将 int 值从 Activity Fragment 传递到另一个 Fragment

java - 覆盖Android中顶部菜单问题的后退按钮事件

java - 在 Android 上使用 IP 地址连接 WiFi 打印机