我的活动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按下后退时,我会收到MatchFragment。VictorFragment不应该是场景中唯一存在的片段吗?按下它应该退出活动?
删除对addToBackStack()
方法的调用。
当你在FragmentTransaction
上调用它时,你告诉系统你希望它"记住"事务,并允许你通过按下后退按钮"撤消"它。因此,如果不希望后退按钮取消VictorFragment
并重新显示MatchFragment2
,那么您应该不要调用该事务的addToBackStack()
。