我遇到了Fragments
非常奇怪的问题,并且导致强制关闭但未遵循逻辑模式的方向更改。
我创建了一个简单的Activity
和Fragment
生命周期调试应用程序,它通过报告对logcat的调用来简单地实现Activity lifecycle和Fragment lifecycle的每一步。
以下是TestActivity
和TestFragment
类:
测试活动
public class TestActivity extends Activity {
Context ct = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("ACT", "onCreate called");
ct = getApplicationContext();
FrameLayout fl = new FrameLayout(ct);
fl.setId(1000);
TestFragment tf = new TestFragment();
getFragmentManager().beginTransaction().add(fl.getId(), tf, "").commit();
setContentView(fl);
}
@Override
protected void onStart() {
Log.e("ACT", "onStart called");
super.onStart();
}
@Override
protected void onResume() {
Log.e("ACT", "onResume called");
super.onResume();
}
@Override
protected void onPause() {
Log.e("ACT", "onPause called");
super.onPause();
}
@Override
protected void onStop() {
Log.e("ACT", "onStop called");
super.onStop();
}
@Override
protected void onDestroy() {
Log.e("ACT", "onDestroy called");
super.onDestroy();
}
@Override
protected void onRestart() {
Log.e("ACT", "onRestart called");
super.onRestart();
}
}
TestFragment
public class TestFragment extends Fragment {
Context ctFrag = null;
@Override
public void onAttach(Activity activity) {
Log.e("FRAG", "onAttach called");
super.onAttach(activity);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("FRAG", "onCreate called");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.e("FRAG", "onCreateView called");
ctFrag = ((TestActivity) getActivity()).ct;
TextView tv = new TextView(ctFrag);
tv.setText("My test TextView");
return tv;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
Log.e("FRAG", "onActivityCreated called");
super.onActivityCreated(savedInstanceState);
}
@Override
public void onStart() {
Log.e("FRAG", "onStart called");
super.onStart();
}
@Override
public void onResume() {
Log.e("FRAG", "onResume called");
super.onResume();
}
@Override
public void onPause() {
Log.e("FRAG", "onPause called");
super.onPause();
}
@Override
public void onStop() {
Log.e("FRAG", "onStop called");
super.onStop();
}
@Override
public void onDestroyView() {
Log.e("FRAG", "onDestroyView called");
super.onDestroyView();
}
@Override
public void onDestroy() {
Log.e("FRAG", "onDestroy called");
super.onDestroy();
}
@Override
public void onDetach() {
Log.e("FRAG", "onDetach called");
super.onDetach();
}
}
初始化时,Logcat输出遵循预期的顺序(开始Activity
,附加Fragment
,生命周期调用发生等):
01-29 10:12:50.270: E/ACT(3321): onCreate called
01-29 10:12:50.760: E/FRAG(3321): onAttach called
01-29 10:12:50.760: E/FRAG(3321): onCreate called
01-29 10:12:50.760: E/FRAG(3321): onCreateView called
01-29 10:12:50.770: E/FRAG(3321): onActivityCreated called
01-29 10:12:50.770: E/ACT(3321): onStart called
01-29 10:12:50.770: E/FRAG(3321): onStart called
01-29 10:12:50.770: E/ACT(3321): onResume called
01-29 10:12:50.770: E/FRAG(3321): onResume called
但问题是,当发生方向更改时,Android Documentation会说:
当发生这样的更改时,Android会重新启动正在运行的Activity(调用onDestroy(),然后调用onCreate())
这表明它应该关闭Activity
及其内容,如生命周期建议(并且确实如此)但是,然后通过相同的有序流程重新创建新方向中的Activity
。此不会发生,似乎尝试重新创建Fragment
,然后然后在活动重新创建中创建新的。{/ p>
01-29 10:17:52.249: E/FRAG(3321): onPause called
01-29 10:17:52.259: E/ACT(3321): onPause called
01-29 10:17:52.269: E/FRAG(3321): onStop called
01-29 10:17:52.269: E/ACT(3321): onStop called
01-29 10:17:52.279: E/FRAG(3321): onDestroyView called
01-29 10:17:52.299: E/FRAG(3321): onDestroy called
01-29 10:17:52.299: E/FRAG(3321): onDetach called
01-29 10:17:52.299: E/ACT(3321): onDestroy called
01-29 10:17:52.650: E/FRAG(3321): onAttach called
01-29 10:17:52.650: E/FRAG(3321): onCreate called
01-29 10:17:52.650: E/ACT(3321): onCreate called
01-29 10:17:53.020: E/FRAG(3321): onCreateView called
01-29 10:17:53.020: E/FRAG(3321): onActivityCreated called
01-29 10:17:53.030: E/FRAG(3321): onAttach called
01-29 10:17:53.030: E/FRAG(3321): onCreate called
01-29 10:17:53.030: E/FRAG(3321): onCreateView called
01-29 10:17:53.030: E/FRAG(3321): onActivityCreated called
01-29 10:17:53.060: E/ACT(3321): onStart called
01-29 10:17:53.060: E/FRAG(3321): onStart called
01-29 10:17:53.060: E/FRAG(3321): onStart called
01-29 10:17:53.060: E/ACT(3321): onResume called
01-29 10:17:53.060: E/FRAG(3321): onResume called
01-29 10:17:53.060: E/FRAG(3321): onResume called
显然有很多解决方案可以解决这个问题,但我的问题是为什么会发生这种情况?为什么Fragment
引用在Activity
被认为是完全被破坏和重新创建的那部分时被维护和重新创建?我可以证明Fragment
故意将其与活动分开。但导致这些问题的原因是为什么是原始Fragment
附加并重新创建之前 Activity
?它似乎并不遵循Android进程其余部分所遵循的逻辑生命周期。
答案 0 :(得分:10)
这是因为活动在被销毁之前调用onSaveInstanceState(Bundle)
。默认情况下,活动正在此方法中保存其片段的状态。
稍后,当重新创建活动时,将在活动onCreate(Bundle savedInstanceState)
方法中重新创建旧片段。
答案 1 :(得分:1)
这是因为您在重新创建活动时一次又一次地添加片段。您可以在activity的onCreate方法中使用以下代码,以避免重新创建片段:
if(savedInstanceState == null)
{
mFragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
FragmentOne fragment = new FragmentOne();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();
}
当配置更改发生时,旧的片段不会被销毁 - 它会在重新创建时将其自身添加回活动。