我在一个活动中,我从那里启动一个片段,它出现在屏幕上很好,但我想使用按钮然后删除相同的片段。我看不出代码有什么问题,我知道它正在进入应该删除它的代码,因为 toast 说“删除片段”。有任何想法吗?谢谢
public class ButtonClickHandler implements View.OnClickListener {
public void onClick(View view) {
if (showFrag == true){
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
MyFragment myFragment = new MyFragment();
fragmentTransaction.add(R.id.myfragment, myFragment);
fragmentTransaction.commit();
showFrag = false;
Toast.makeText(getBaseContext(), "Show Fragment", Toast.LENGTH_SHORT).show();
}
else if (showFrag == false){
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
getSupportFragmentManager().popBackStack();
fragmentTransaction.commit();
Toast.makeText(getBaseContext(), "Remove Fragment", Toast.LENGTH_SHORT).show();
showFrag = true;
}
}
}