android 不保留活动,关于“不保留活动”时 Fragment的数据还原问题

这篇博客探讨了在Android开发中如何正确使用Fragment管理器。主要关注点在于使用getChildFragmentManager()方法来管理Fragment内嵌套的Fragment,以及getFragmentManager()和getSupportFragmentManager()的区别。作者强调了在Fragment嵌套场景下使用getChildFragmentManager()的重要性,并提供了示例代码展示如何初始化和替换Fragment。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.fragment里嵌套fragment时,最好使用getChildFragmentManager()方法来获取FragmentManager。比较没有坑

2.获取FragmentManager的方法有3中:

getChildFragmentManager();(只用于Fragment)

getFragmentManager();(只用于非FragmentActivity的Activity)

getSupportFragmentManager();(只用于FragmentActivity的Activity)

3.合理使用Tag

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_camera);

NativeLibrary.ndkInit(this);

mCameraFragment = (CameraFragment) getSupportFragmentManager().findFragmentByTag(CameraFragment.TAG);

if (mCameraFragment == null) {

mCameraFragment = new CameraFragment();

Bundle bundle = new Bundle();

bundle.putParcelable(CameraFragment.ACTIVITY_INTENT, getIntent());

mCameraFragment.setArguments(bundle);

}

getSupportFragmentManager().beginTransaction()

.replace(R.id.rl_root, mCameraFragment, CameraFragment.TAG)

.commitAllowingStateLoss();

}

4.关于getFragmentManager()与getChildFragmentManager():

// Fragment.java

// The fragment manager we are associated with. Set as soon as the

// fragment is used in a transaction; cleared after it has been removed

// from all transactions.

FragmentManagerImpl mFragmentManager;

/**

* Return the FragmentManager for interacting with fragments associated

* with this fragment's activity. Note that this will be non-null slightly

* before {@link #getActivity()}, during the time from when the fragment is

* placed in a {@link FragmentTransaction} until it is committed and

* attached to its activity.

*

*

If this Fragment is a child of another Fragment, the FragmentManager

* returned here will be the parent's {@link #getChildFragmentManager()}.

*/

final public FragmentManager getFragmentManager() {

return mFragmentManager;

}

// Fragment.java

// Private fragment manager for child fragments inside of this one.

FragmentManagerImpl mChildFragmentManager;

/**

* Return a private FragmentManager for placing and managing Fragments

* inside of this Fragment.

*/

final public FragmentManager getChildFragmentManager() {

if (mChildFragmentManager == null) {

instantiateChildFragmentManager();

if (mState >= RESUMED) {

mChildFragmentManager.dispatchResume();

} else if (mState >= STARTED) {

mChildFragmentManager.dispatchStart();

} else if (mState >= ACTIVITY_CREATED) {

mChildFragmentManager.dispatchActivityCreated();

} else if (mState >= CREATED) {

mChildFragmentManager.dispatchCreate();

}

}

return mChildFragmentManager;

}

void instantiateChildFragmentManager() {

if (mHost == null) {

throw new IllegalStateException("Fragment has not been attached yet.");

}

mChildFragmentManager = new FragmentManagerImpl();

mChildFragmentManager.attachController(mHost, new FragmentContainer() {

@Override

@Nullable

public View onFindViewById(int id) {

if (mView == null) {

throw new IllegalStateException("Fragment does not have a view");

}

return mView.findViewById(id);

}

@Override

public boolean onHasView() {

return (mView != null);

}

}, this);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值