在使用getSupportFragmentManager().findFragmentByTag(tag)无法找到,返回为空,原因由于咋replace()时必须要将fragment放入到返回栈中。
保证调用以下方法:ft.addToBackStack(tag);
/**
* Example of starting nested fragment from another fragment: Fragment
* newFragment = ManagerTagFragment.newInstance(tag.getMac()); TagsActivity
* tAct = (TagsActivity)getActivity(); tAct.pushFragments(newFragment, true,
* true, null);
* --------------------------------------------------------------
* fragement切换
*
* @param fragment
* 要切换的界面
* @param shouldAnimate
* 是否需要动画
* @param addToBackStack
* 是否添加到返回栈中(只有添加到返回栈中的界面
* 才可以使用getSupportFragmentManager().findFragmentByTag
* (setting))找到对应的界面(使用返回键返回后,栈中缓存的界面回收)
* @param tag
* 界面标志
*/
public void pushFragments(Fragment fragment, boolean shouldAnimate,
boolean addToBackStack, String tag) {
FragmentManager manager = getSupportFragmentManager();// getFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
if (shouldAnimate) {
// ft.setCustomAnimations(R.animator.fragment_slide_left_enter,
// R.animator.fragment_slide_left_exit,
// R.animator.fragment_slide_right_enter,
// R.animator.fragment_slide_right_exit);
}
ft.replace(R.id.sdk_main_content, fragment, tag);
// ft.attach(fragment);
if (addToBackStack) {
/*
* here you can create named backstack for realize another logic.
* ft.addToBackStack("name of your backstack");
*/
ft.addToBackStack(tag);
} else {
/*
* and remove named backstack:
* manager.popBackStack("name of your backstack",
* FragmentManager.POP_BACK_STACK_INCLUSIVE); or remove whole:
* manager.popBackStack(null,
* FragmentManager.POP_BACK_STACK_INCLUSIVE);
*/
manager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
}
ft.commit();
}
</pre><pre name="code" class="java">
</pre><pre name="code" class="java">