我有一个活动和许多碎片在同一个膨胀 FrameLayout
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Run Code Online (Sandbox Code Playgroud)
示例:mainActivity>任何片段(按后退按钮)>活动为空.
在onCreate:
layout = (FrameLayout)findViewById(R.id.content_frame);
layout.setVisibility(View.GONE);
Run Code Online (Sandbox Code Playgroud)
当我开始片段时:
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, profileFragment);
ft.addToBackStack(null);
ft.commit();
layout.setVisibility(View.VISIBLE);
Run Code Online (Sandbox Code Playgroud)
我想我需要GONE在后退时再次使frameLayout的可见性,但我该怎么做?
我尝试onBackPressed并设置layout.setVisibility(View.GONE);但我无法通过片段返回,因为我直接进入主页.
我现在有一个包含片段的活动
[1],[2],[3],[4]
如果按下按钮[3],可以将其重定向到[4]
我想实现后退按钮如下图所示..
当按下[4]时,返回[3]
当按下[3]时,返回[2]
当按下[1]时,活动结束();
当涉及到当前的实现时,它完成活动而不是弹出Fragment.你能告诉我我应该做什么或记住什么?
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if( keyCode==KeyEvent.KEYCODE_BACK)
{
finish();
}
return super.onKeyDown(keyCode, event);
}
Run Code Online (Sandbox Code Playgroud) 如果它有帮助,我想要的东西与谷歌教程中的内容类似
但是在转换之前会创建一个片段.如果我这样做,过渡工作正常; 但我不能用这种方法
=====
针对API 7+,我只想在整个屏幕中看到一个片段,并使用一个按钮(一个带有onTouch事件的绘制按钮),然后交替使用第二个片段,反之亦然.
但是当我用第二个片段替换第一个片段时,或者如果我使用fragmentTransaction.show和fragmentTransaction.hide,我得到一个空白屏幕; 在我得到空白屏幕之前,我可以切换两次.我不想在后台上.
我在MainActivity的onCreate中创建片段:
DiceTable diceTable = new DiceTable();
Logger logger = new Logger();
fragmentTransaction.add(diceTable, DICETABLE_TAG);
fragmentTransaction.add(logger, LOGGER_TAG);
fragmentTransaction.add(R.id.fragment_container, logger);
fragmentTransaction.add(R.id.fragment_container, diceTable);
Run Code Online (Sandbox Code Playgroud)
然后在一个方法(从片段调用)我做切换:
Logger logger = (Logger)fragmentManager.findFragmentByTag(LOGGER_TAG);
DiceTable diceTable = (DiceTable)fragmentManager.findFragmentByTag(DICETABLE_TAG);
if (diceTable.isVisible()) {
fragmentTransaction.replace(R.id.fragment_container, logger);
fragmentTransaction.commit();
fragmentTransaction.hide(diceTable);
fragmentTransaction.show(logger);
}
else if (logger.isVisible()) {
fragmentTransaction.replace(R.id.fragment_container, diceTable);
fragmentTransaction.commit();
fragmentTransaction.hide(logger);
fragmentTransaction.show(diceTable);
}
Run Code Online (Sandbox Code Playgroud)
这不是我应该怎么做的?
更换碎片时出现空白屏幕