删除片段仍然可见

时间:2016-01-04 09:12:47

标签: android android-fragments

我正在开发一个自助服务终端应用程序,以便它接管平板电脑。单一活动与一堆不同的片段。我在活动的oncreate中提交了一个片段。然后当设备连接到蓝牙时,它将通过删除旧片段并添加新片段来加载新片段(调用函数cancelFunc())。我遇到的问题是第一个片段在删除后仍然可见。我的日志显示它实际上被删除了,而是附加了一个新的片段,但我看不到新的或随后附加的任何其他片段。但是,当我点击屏幕时,它会显示新片段。有点奇怪,我想知道我做错了什么。它只发生在附加在活动的oncreate上的片段上。代码很长,所以我只是向您展示一些重要的部分。在此先感谢您的帮助。

@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.d(TAG, "onCreate()");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.backbone);

    FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    ft.add(R.id.fragmentContainer, new SystemDownFragment(), "SystemDownFragment");
    ft.commit();

    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    // If the adapter is null, then Bluetooth is not supported
    if (mBluetoothAdapter == null) {
        Log.d(TAG, "adapter is null");
        Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
        finish();
    }

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_FULLSCREEN
            | View.SYSTEM_UI_FLAG_IMMERSIVE);
}

public void cancelFunc() {
    Log.d(TAG, "cancelFunc()");

    removeFrag();

    FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    ft.add(R.id.fragmentContainer, new StartupFragment(), "StartupFragment");
    ft.commit();
    getFragmentManager().executePendingTransactions();
}

private void removeFrag(){
    Log.d(TAG, "removeFrag()" + getActiveFragments().size());
    if(getActiveFragments().size() > 0) {
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        for (int i = 0; i < getActiveFragments().size(); i++) {
            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
            ft.remove(getActiveFragments().get(i));
        }
        ft.commit();
    }
}

@Override
public void onAttachFragment (Fragment fragment) {
    Log.d(TAG, "onAttachFragment");
    fragList.add(new WeakReference(fragment));
}

public List<Fragment> getActiveFragments() {
    ArrayList<Fragment> ret = new ArrayList<Fragment>();
    for(WeakReference<Fragment> ref : fragList) {
        Fragment f = ref.get();
        if(f != null) {
            if(f.isVisible()) {
                ret.add(f);
            }
        }
    }
    return ret;
}

我记录的数据显示以下内容:

MainActivity: onCreate()
MainActivity: onAttachFragment
MainActivity: onStart()
MainActivity: onResume()
MainActivity: Bluetooth Connected
MainActivity: cancelFunc()
MainActivity: removeFrag()1
MainActivity: onAttachFragment

4 个答案:

答案 0 :(得分:2)

清除或删除当前片段中的所有视图,然后替换Layout中的新片段。

喜欢这个,试试这个

myLayout.removeAllViews();

AddNewFragment new = new AddNewFragment();

this.getFragmentManager().beginTransaction().replace(R.id.fragment_mainLayout, new, "").addToBackStack(null).commit();

答案 1 :(得分:0)

为了删除片段,您只需要调用活动onBackPressed()方法,因为此方法执行返回操作,因此它将从活动中删除当前片段,然后您可以添加另一个。所以用下面的代码更新你的removefrag()方法。

试试这个,

void removeFrag(){
   onBackPresssed();
}

答案 2 :(得分:0)

它可能会解决您的问题,但它也是您想要做的好事;在FragmentTransaction上使用// we know for sure the regex is a valid string, thus the forced try, otherwise forced stuff is not recommended let regex = try! NSRegularExpression(pattern: "\\<\\!\\[CDATA\\[|\\]\\]", options: NSRegularExpressionOptions.CaseInsensitive) let len = xmlvar.lengthOfBytesUsingEncoding(NSStringEncoding(NSUTF8StringEncoding)) let processedString = regex.stringByReplacingMatchesInString(xmlvar, options: NSMatchingOptions(rawValue: 0), range: NSMakeRange(0, len), withTemplate: "") print(processedString) 而不是添加和删除...

答案 3 :(得分:0)

在该活动中按下后,您必须替换新片段。

 public void onBackPressed() {
        ft.replace(R.id.fragment_container, new_fragment);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        ft.commit();

}