savedInstanceState!=null
后 setRetainInstance(true)
这是预期的行为吗?
我有一个带有ParentFragment的MainActivity,而ParentFragment拥有另一个ChildFragment
public class MainActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
recLifeCycle_with_savedInstanceState(savedInstanceState);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
// if's the first time created
ParentFragment parentFragment = new ParentFragment();
FragmentManager supportFragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
fragmentTransaction.add(R.id.container, parentFragment, ParentFragment.TAG);
fragmentTransaction.commit();
supportFragmentManager.executePendingTransactions();
}
}
}
public class ParentFragment extends BaseFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
recLifeCycle_with_savedInstanceState(savedInstanceState);
View root = inflater.inflate(R.layout.fragment_parent, container, false);
if(savedInstanceState==null){
ChildFragment childFragment = new ChildFragment();
FragmentManager childFragmentManager = getNestedChildFragmentManager();
FragmentTransaction fragmentTransaction = childFragmentManager.beginTransaction();
fragmentTransaction.add(R.id.parent_container, childFragment, ChildFragment.TAG);
fragmentTransaction.commit();
childFragmentManager.executePendingTransactions();
}
recLifeCycle(sampleString);
return root;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
recLifeCycle_with_savedInstanceState(savedInstanceState);
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
}
}
public class ChildFragment extends BaseFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
recLifeCycle(sampleString);
recLifeCycle_with_savedInstanceState(savedInstanceState);
return inflater.inflate(R.layout.fragment_child, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
recLifeCycle_with_savedInstanceState(savedInstanceState);
super.onActivityCreated(savedInstanceState);
}
}
public class BaseFragment extends Fragment {
private FragmentManager mRetainedChildFragmentManager;
public FragmentManager getNestedChildFragmentManager() {
if(mRetainedChildFragmentManager == null) {
mRetainedChildFragmentManager = getChildFragmentManager();
}
return mRetainedChildFragmentManager;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (mRetainedChildFragmentManager != null) {
//restore the last retained child fragment manager to the new
//created fragment
try {
Field childFMField = Fragment.class.getDeclaredField("mChildFragmentManager");
childFMField.setAccessible(true);
childFMField.set(this, mRetainedChildFragmentManager);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
观察到的生命周期日志如下
I/MYTAG: MainActivity.onCreate / savedInstanceState == null
I/MYTAG: ParentFragment.onAttach
I/MYTAG: ParentFragment.onCreate / savedInstanceState == null
I/MYTAG: MainActivity.onStart
I/MYTAG: ParentFragment.onCreateView / savedInstanceState == null
I/MYTAG: ChildFragment.onAttach
I/MYTAG: ChildFragment.onCreate / savedInstanceState == null
I/MYTAG: ParentFragment.onCreateView / 0.9485654467684422
I/MYTAG: ParentFragment.onActivityCreated / savedInstanceState == null
I/MYTAG: ChildFragment.onCreateView / 0.267993285910222
I/MYTAG: ChildFragment.onCreateView / savedInstanceState == null
I/MYTAG: ChildFragment.onActivityCreated / savedInstanceState == null
I/MYTAG: ParentFragment.onStart
I/MYTAG: ChildFragment.onStart
I/MYTAG: MainActivity.onResume
I/MYTAG: ParentFragment.onResume
I/MYTAG: ChildFragment.onResume
当屏幕旋转时,我发现父片段的savedinstancestate不为null,但是将子片段的实例状态保存为null
I/MYTAG: MainActivity.onPause
I/MYTAG: ChildFragment.onPause
I/MYTAG: ParentFragment.onPause
I/MYTAG: MainActivity.onStop
I/MYTAG: ChildFragment.onStop
I/MYTAG: ParentFragment.onStop
I/MYTAG: MainActivity.onDestroy
I/MYTAG: ChildFragment.onDestroyView
I/MYTAG: ParentFragment.onDestroyView
I/MYTAG: ParentFragment.onDetach
I/MYTAG: MainActivity.onCreate / savedInstanceState != null
I/MYTAG: ParentFragment.onAttach
I/MYTAG: MainActivity.onStart
I/MYTAG: ParentFragment.onCreateView / savedInstanceState != null
I/MYTAG: ParentFragment.onCreateView / 0.9485654467684422
I/MYTAG: ParentFragment.onActivityCreated / savedInstanceState != null
I/MYTAG: ChildFragment.onCreateView / 0.267993285910222
I/MYTAG: ChildFragment.onCreateView / savedInstanceState == null
I/MYTAG: ChildFragment.onActivityCreated / savedInstanceState == null
I/MYTAG: ParentFragment.onStart
I/MYTAG: ChildFragment.onStart
I/MYTAG: MainActivity.onResume
I/MYTAG: ParentFragment.onResume
I/MYTAG: ChildFragment.onResume