信息:我有一个2面板布局(2子Fragments
一中) ParentFragment
,这当然是一个内部FragmentActivity
。 我有setRetainInstance(true)
的ParentFragment
。 在改变方向时左子片段不被摧毁( onCreate()
不会被调用),这是正常的(因为母公司保持其实例)。
问题:在方向变化, 正确的片段被销毁( onCreate()
被调用)。 凭啥是正确的片段破坏,左侧是不是?
编辑:如果我删除setRetainInstance(true)
,然后左片段的onCreate()
被调用两次 (笑WTF)和右片段的onCreate()
被调用一次。 因此,这也不好...
下面的代码为ParentFragment:
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.setRetainInstance(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_schedule, container, false);
setHasOptionsMenu(true);
if (getChildFragmentManager().findFragmentById(R.id.fragment_schedule_framelayout_left) == null ||
!getChildFragmentManager().findFragmentById(R.id.fragment_schedule_framelayout_left).isInLayout())
{
if (mPresentationsListFragment == null)
mPresentationsListFragment = PresentationsListFragment.newInstance(PresentationsListFragment.TYPE_SCHEDULE, mScheduleDate);
getChildFragmentManager().beginTransaction()
.replace(R.id.fragment_schedule_framelayout_left, mPresentationsListFragment)
.commit();
}
mPresentationsListFragment.setOnPresentationClickListener(this);
return view;
}
@Override
public void onPresentationClick(int id)
{
if (Application.isDeviceTablet(getActivity()))
{
if (getChildFragmentManager().findFragmentById(R.id.fragment_schedule_framelayout_right) == null)
{
if (mPresentationDetailFragment == null)
mPresentationDetailFragment = PresentationDetailFragment.newInstance(id);
else
mPresentationDetailFragment.loadPresentation(id);
getChildFragmentManager().beginTransaction()
.replace(R.id.fragment_schedule_framelayout_right, mPresentationDetailFragment)
.commit();
}
else
mPresentationDetailFragment.loadPresentation(id);
}
else
{
Intent presentationDetailIntent = new Intent(getActivity(), PresentationDetailActivity.class);
presentationDetailIntent.putExtra(PresentationDetailActivity.KEY_PRESENTATION_ID, id);
startActivity(presentationDetailIntent);
}
}
LE解决方案:由于很多antonyt,答案在下面。 需要PE执行的唯一变化驻留内的父onCreateView() Fragment
。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_schedule, container, false);
setHasOptionsMenu(true);
if (getChildFragmentManager().findFragmentById(R.id.fragment_presentations_framelayout_left) == null)
{
mPresentationsListFragment = PresentationsListFragment.newInstance();
mPresentationsListFragment.setOnPresentationClickListener(this);
getChildFragmentManager().beginTransaction()
.add(R.id.fragment_presentations_framelayout_left, mPresentationsListFragment)
.commit();
}
return view;
}