我有一个 Activity 和许多在同一个 FrameLayout
<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/