假设在我的应用中有几个可能导航流程(全部都是片段
)
A -> B -> C -> D -> E
A -> F -> B -> C
我希望能够无论事务回退栈的深度如何都返回到片段
B
。我不想跟踪当前显示的 E
或 C
。我注意到可以为片段打标签,但以下代码似乎不起作用:在
片段 A
中创建片段 B
,也称为 SocialViewFragment
:FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// Method 1
transaction.add(R.id.fragment_container, frag, SocialViewFragment.FRAG_TAG).commit();
// Method 2
//transaction.replace(R.id.fragment_container, frag);
//transaction.addToBackStack(SocialViewFragment.FRAG_TAG).commit();
然后在
片段E
中,popBackStack
返回false(什么也不做),因为它找不到标签?!FragmentManager mgr = PlaybackFrag.this.getActivity().getSupportFragmentManager();
if (mgr.getBackStackEntryCount() > 0) {
// Want to go back to SocialViewFragment !!!
mgr.popBackStack(SocialViewFragment.FRAG_TAG, 0); // returns False - can't find the tag!
}