我有2个片段和1个活动。
我打算通过这样的活动从第一个片段中删除第二个片段:
片段1:
FragmentManager manager = getActivity().getSupportFragmentManager();
if(manager.findFragmentByTag("AdFragment") != null){
manager.beginTransaction().remove(manager.findFragmentByTag("AdFragment")).commit();
Log.d("BACK", "Removed AdFragment");
}
现在当我回来按下我的onBackPressed方法时,活动仍然找到刚删除的第二个片段:
的活动:
@Override
public void onBackPressed() {
FragmentManager manager = getSupportFragmentManager();
AdFragment adFrag = (AdFragment) manager.findFragmentByTag("AdFragment");
if(adFrag != null){
Log.d("BACK", "Found AdFragment from backstack");
}
}
即使我删除片段1中的片段,也会找到AdFragment。我有什么误解? 谢谢!
这是从活动开始第二个片段的时刻:
public void openAdFragment(View v){
final AdFragment adFragment = new AdFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.adFragmentContainer, adFragment, "AdFragment").addToBackStack("AdFragment").commit();
}
以下是启动第一个片段的代码:
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, buildingFragment, "BUILDINGPAGE");