Android - onCreateView被称为onBackPressed

时间:2016-03-01 14:09:29

标签: android android-fragments

我有两个片段的活动。在这一个片段中,另一个片段被替换。我已经将两个片段都添加到了Backstack中。

  @Override
public void showGatherIntentions() {

    gatherIntentionView = GatherIntentionsFragment.newInstance(GatherIntentionContract.INTENTION_OPTION_NONE);
    FragmentManager manager = getSupportFragmentManager();
    boolean fragmentPopped = manager.popBackStackImmediate (GATHER_INTENTIONS, 0);


    if (!fragmentPopped) {
        getSupportFragmentManager().beginTransaction()
                .setCustomAnimations(0, 0, R.anim.slide_in_right, R.anim.slide_out_right)
                .replace(R.id.contentFrame, (Fragment) gatherIntentionView)
                .addToBackStack(GATHER_INTENTIONS)
                .commit();
        currentPosition++;
    }
}    

替换第一个片段的代码

 @Override
public void showCollectPreferences() {
    collectPreferencesView = CollectPreferencesFragment.newInstance(CollectPreferencesContract.DEFAULT_OPTION);
    getSupportFragmentManager().beginTransaction()
            .setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_left,
                    R.anim.slide_in_right, R.anim.slide_out_right)
            .replace(R.id.contentFrame, (Fragment) collectPreferencesView)
            .addToBackStack(COLLECT_PREFERENCES)
            .commit();
    currentPosition++;
}

替换第二个的代码

  @Override
public void onBackPressed() {
    FragmentManager fm = getSupportFragmentManager();
    if (fm.getBackStackEntryCount() == 1) {
        finish();
    } else {
        super.onBackPressed();
        currentPosition--;
    }
}

状态被保存为第二个片段但不是第一个片段,即每当我在第二个片段时按回按钮,它的onCreateView方法被调用。

任何帮助将不胜感激。提前谢谢!

0 个答案:

没有答案