在碎片上的后退按钮pressed活动是空白 [英] In Fragment on back button pressed Activity is blank

查看:240
本文介绍了在碎片上的后退按钮pressed活动是空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动,许多片段充气同一的FrameLayout

I have an Activity and many fragments inflated in same FrameLayout

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

例如:mainActivity>的任何片段(preSS后退键)>活动是空白

example: mainActivity > any fragment (press back button) > activity is blank.

在的onCreate:

In onCreate:

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

当我开始一个片段:

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

我想我需要做的FrameLayout的知名度 GONE 再次回到pressed,但如何做到这一点?

I suppose I need to make the frameLayout's visibility GONE again on back pressed, but how do I do this?

我试过 onBack pressed ,并设置 layout.setVisibility(View.GONE); ,但我不能回去通过片段,我直接进入主页面。

I tried onBackPressed and set layout.setVisibility(View.GONE); but I cannot go back through fragments, as I go directly to main page.

推荐答案

如果你有一个以上的片段被用在活动,甚至如果你只有一个片段,然后第一个片段应该不会有addToBackStack定义。因为这允许后退导航和在此之前的片段的空活动布局将被显示。

If you have more than one fragment been used in the activity or even if you have only one fragment then the first fragment should not have addToBackStack defined. Since this allows back navigation and prior to this fragment the empty activity layout will be displayed.

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

但你需要有这样的定义,否则另一个片段后面会不会浏览到先前屏幕(片段),而不是应用程序可能会关闭。

But for the other fragment you need to have this defined otherwise the back will not navigate to earlier screen (fragment) instead the application might shutdown.

这篇关于在碎片上的后退按钮pressed活动是空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆