【问题标题】:Fragment is still visible after remove method删除方法后片段仍然可见
【发布时间】:2014-03-15 11:36:08
【问题描述】:

我的程序中的片段有问题 - 在 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 - 所以片段是未触及的,但仍然可见。不知道如何“杀死”这个问题(((

【问题讨论】:

    标签: android android-fragmentactivity


    【解决方案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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多