android - 在按下后退按钮的 fragment 中, Activity 为空白

标签 android android-fragments

我有一个 Activity 和许多在同一个 FrameLayout

中膨胀的 fragment
<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

示例:mainActivity > 任何 fragment (按返回按​​钮)> Activity 为空白。

在 onCreate 中:

layout = (FrameLayout)findViewById(R.id.content_frame);
layout.setVisibility(View.GONE);

当我开始一个 fragment 时:

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, profileFragment);
ft.addToBackStack(null);
ft.commit();
layout.setVisibility(View.VISIBLE);

我想我需要在后按时再次使 frameLayout 的可见性 GONE,但是我该怎么做呢?


我尝试了 onBackPressed 并设置了 layout.setVisibility(View.GONE); 但我无法通过 fragment 返回,因为我直接进入主页。

最佳答案

如果您在 Activity 中使用了多个 fragment ,或者即使您只有一个 fragment ,那么第一个 fragment 不应该定义 addToBackStack。由于这允许返回导航,并且在此 fragment 之前将显示空的 Activity 布局。

 // fragmentTransaction.addToBackStack() // dont include this for your first fragment.

但是对于其他 fragment ,您需要定义它,否则后面将不会导航到较早的屏幕( fragment ),而是应用程序可能会关闭。

关于android - 在按下后退按钮的 fragment 中, Activity 为空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20340303/

相关文章:

android - 几何概念的解释

android - 应用程序启动时应用程序类不运行?

android - 防止最后一个 Fragment 从 backstack 中删除

java - 调用函数/方法时出错?

java - 使用 fragment 显示用户安装的应用程序

android - 使用 CoordinatorLayout 隐藏工具栏,但在 fragment 上隐藏 RecyclerView

android - 如何在 Deeplink 消息中添加自定义文本而不是 URL

android - 包括一个背景可绘制的 foobars 我在 Android 中的布局。想法为什么?

android - echo $ANDROID_HOME 在 Mac 中返回空白

android - 方向改变时未加载不同的 fragment (android)