删除片段并移至上一个片段

时间:2014-01-02 11:34:31

标签: android android-layout

片段" A"调用片段" B"反过来,调用片段" C"。

现在,我希望片段中的按钮点击事件" C"片段" C"应该删除并分段" B"应该显示。(所有碎片已经进入了Backstack。我遇到了一些问题。)帮助!

3 个答案:

答案 0 :(得分:1)

你试过getSupportFragmentManager().popBackStackImmediate();吗?(假设你正在使用ABS)。

答案 1 :(得分:1)

您可以使用 popBackStack(); 方法:

但是你可以使用这种正确的方式使用Fragment:

private static final String STATE_URI = "state:uri";
private static final String STATE_FRAGMENT_TAG = "state:fragment_tag";
private Uri currentUri = YourFragment.ABOUT_URI;
private String currentContentFragmentTag = null;

on Oncreate()方法:

if (savedInstanceState != null) {
        currentUri = Uri.parse(savedInstanceState.getString(STATE_URI));
        currentContentFragmentTag = savedInstanceState
                .getString(STATE_FRAGMENT_TAG);
    }


   @Override
protected void onSaveInstanceState(Bundle outState) {
    outState.putString(STATE_URI, currentUri.toString());
    outState.putString(STATE_FRAGMENT_TAG, currentContentFragmentTag);

    super.onSaveInstanceState(outState);
}


   @Override
   public void onBackPressed() {
    final Fragment currentFragment = getSupportFragmentManager()
            .findFragmentByTag(currentContentFragmentTag);
    if (currentFragment instanceof YourDesiredFragment) {
        final YourDesiredFragment webFragment = (YourDesiredFragment) currentFragment;
        if (webFragment.onBackPressed())
            return;
    }

    super.onBackPressed();
}

答案 2 :(得分:0)

我假设您已经尝试popBackStack