删除方法后片段仍然可见

时间:2014-03-15 11:36:08

标签: android android-fragmentactivity

我的程序中存在片段问题 - 在 getFragmentManager()。beginTransaction()。remove(selectSectionFragment).commit()方法后仍然可见。但是当我在模拟器中更改屏幕模式(例如 - 横向)时 - 片段消失了(我需要的东西)。

公共类AddCardActivity扩展了Activity {

private EditText sectionChoiceField;
private android.app.FragmentTransaction fTrans;
private SelectSectionFragment selectSectionFragment;

private String selectedSectionName;
private int selectedSectionId;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_card);

    TextView topTitle = (TextView)findViewById(R.id.addCardTextView);       
    Typeface font = Typeface.createFromAsset(getAssets(), "fonts/mainmenu_button_font.ttf");
    topTitle.setTypeface(font);

    sectionChoiceField = (EditText)findViewById(R.id.sectionChoice);
    selectSectionFragment = new SelectSectionFragment();

    sectionChoiceField.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {   
            fTrans = getFragmentManager().beginTransaction();   
            fTrans.add(R.id.add_card_container, selectSectionFragment);
            fTrans.setCustomAnimations(R.anim.slide_in, R.anim.slide_out);
            fTrans.show(selectSectionFragment);
            fTrans.commit();
        }
    });
}

public void setSectionToField(String name, int id) {
    sectionChoiceField.setText(name);
    selectedSectionName = name;
    selectedSectionId = id; 
    getFragmentManager().beginTransaction().remove(selectSectionFragment).commit();
}

}

正如您在代码块中看到的那样 - 首先成功执行。

fTrans = getFragmentManager().beginTransaction();   
            fTrans.add(R.id.add_card_container, selectSectionFragment);
            fTrans.setCustomAnimations(R.anim.slide_in, R.anim.slide_out);
            fTrans.show(selectSectionFragment);
            fTrans.commit();

但是setSectionToField方法的执行 - 不会破坏活动中的片段 - 它仍然是可见的,我可以用它进行操作。但是getBackStackEntryCount()返回0 - 所以片段是未触动的,但仍然可见。不知道如何“杀死”这个问题((

1 个答案:

答案 0 :(得分:1)

您必须指定要删除的SelectSectionFragment,因为您已将其添加到R.id.add_card_container,请尝试:

SelectSectionFragment frag = (SelectSectionFragment)getFragmentManager.findFragmentById(R.id.add_card_container);
fTrans = getFragmentManager().beginTransaction();
fTrans.remove(frag); fTrans.commit();
相关问题