我有一个问题是通过id获取当前片段 用标签替换片段后 当试图获得相同的片段时,我发现它为空
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.replace(R.id.request_frame, requestFragment, "Confirm").commit();
RequestFragment requestFragment1=(RequestFragment) getActivity().getSupportFragmentManager().findFragmentByTag("Confirm");
if (null!=requestFragment1)
{
Toast.makeText(getActivity(), "framgent found", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(getActivity(), "framgent Not found", Toast.LENGTH_SHORT).show();
}
总是收到片段未找到
答案 0 :(得分:0)
试试这个
Fragment fragment = getActivity().getSupportFragmentManager().findFragmentById(R.id.YOUR_CONTAINER);
if (fragment != null && fragment.equals(RequestFragment.class.getSimpleName())) {
RequestFragment requestFragment1 = (RequestFragment) fragment;
Toast.makeText(getActivity(), "framgent found", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getActivity(), "framgent Not found", Toast.LENGTH_SHORT).show();
}