我有一个活动的XML,我有一个android:id =“@ + id / fragment_container”FrameLayout,我用来添加片段。
我目前有两个片段
[GameManager],[PlayerStatus]
以下是带容器的XML片段
name
我从主要活动中添加片段[GameManager],如下所示:
id
在GameManager Fragment类中,当按下按钮时,我按如下方式向容器添加一个新片段:
<label :for="choice.value">
<input type="radio" :value="choice.val" v-model="questions[index].responses" /> {{ choice.text }}
</label>
在GameManager中我按如下方式setContentView,其中localgame.xml是上面引用的XML:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dip" >
一切正常,我按下后退按钮返回第一个片段。
在主要活动中,我有以下代码检查后退按钮:
//Start the fragment
Fragment gameManager = new GameManager();
fragmentManager.beginTransaction()
.replace(R.id.fragment_container,gameManager)
.addToBackStack(null)
.commit();
问题在于,当我按下后退按钮时,背面被正确检测到,我也可以看到有东西要弹出()但是第二个片段没有关闭。它仍然可见。我想要它,以便当我们按片段[playerStatus]中的后退按钮时,我们返回[GameManager]片段。
任何提示都表示赞赏!
编辑:在马丁提出修改后,我得到以下内容:
public boolean buttonPress(Type button) {
boolean canBuild = false;
Player player = board.getCurrentPlayer();
switch (button) {
case INFO:
//INFO IS THE BUTTON THAT IS ALWAYS VISIBLE IN TOP LEFT CORNER
Log.d("myTag", "about to launch PLAYER INFO");
PlayerStatus playerStatus = new PlayerStatus();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, playerStatus);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
break;
.
.
.
答案 0 :(得分:0)
在交易GameManager Fragment
时删除以下行Fragment gameManager = new GameManager();
fragmentManager.beginTransaction()
.replace(R.id.fragment_container,gameManager)
// .addToBackStack(null) remove this line
.commit();
您已在onBackPressed()
@Override
public void onBackPressed() {
Log.d("myTag", "DETECTED BACK");
if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
Log.d("myTag", "THERE WAS SOMETHING IN THE FRAGSTACK");
getSupportFragmentManager().popBackStack();
// getSupportFragmentManager().beginTransaction().commit(); remove this line
} else {
super.onBackPressed();
}
}
答案 1 :(得分:0)
在“家长活动”中尝试此代码
@Override
public void onBackPressed() {
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragment_container);
if (fragment instanceof FragmentOne) {
//here handle fragmentOne backpress
}else if (fragment instanceof FragmentTwo) {
//here handle fragmentTwo backpress
}else {
super.onBackPressed();
}
}
希望这会有所帮助!!!