代码之家  ›  专栏  ›  技术社区  ›  Muhammad Ahmed AbuTalib

片段替换再次引发问题

  •  0
  • Muhammad Ahmed AbuTalib  · 技术社区  · 7 年前

    我的活动XML

    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
    
        <FrameLayout
            android:id="@+id/contentFrame1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"></FrameLayout>
    
    </android.support.design.widget.CoordinatorLayout>
    

    我有一个attachView方法,它从活动的onCreate调用

    @Override
    protected void attachView() {
        // Get the fragment from dagger
        getFragmentManager().
                beginTransaction().
                setCustomAnimations(R.animator.slide_up, 0, 0, R.animator.slide_down).
                replace(R.id.contentFrame1, new MatchFragment2()).
                addToBackStack("MatchFragment").
                commit();
        getFragmentManager().executePendingTransactions();
    
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                getFragmentManager().
                        beginTransaction().
                        setCustomAnimations(R.animator.slide_up, 0, 0, R.animator.slide_down).
                        replace(R.id.contentFrame1, new VictorFragment()).
                        addToBackStack("VictorFragment").
                        commit();
                getFragmentManager().executePendingTransactions();
                getFragmentManager().getBackStackEntryCount(); //this reads 2
            }
        }, 3000);
    }
    

    VictorFragment不应该是场景中唯一存在的片段,并且从中后退应该退出活动吗?

    1 回复  |  直到 7 年前
        1
  •  2
  •   Ben P.    7 年前

    删除您对 addToBackStack() 方法。

    当你打电话给我的时候 FragmentTransaction ,您告诉系统您希望它“记住”事务,并允许您通过按“上一步”按钮“撤消”事务。所以如果你 VictorFragment 并重新显示 MatchFragment2 ,那么你应该 呼叫 addToBackStack()

    推荐文章