我一直遇到非常奇怪的问题Fragments
和方向变化,这些问题导致强制关闭并且不遵循逻辑模式。
我创建了一个简单的Activity
生命周期调试应用程序,它通过向 logcat 报告调用来Fragment
简单地实现Activity 生命周期和Fragment 生命周期的每一步。
这是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();
}
}
测试片段
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 文档说:
当发生这样的变化时,Android会重启正在运行的Activity(onDestroy()被调用,然后是onCreate())
这表明它应该像生命周期所建议的那样关闭Activity
其及其内容(并且确实如此),但然后通过相同的有序过程Activity
以新的方向重新创建。这不会发生,似乎Fragment
试图重新创建,然后在活动重新创建中创建一个新的。
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
维护和重新创建引用?我可以通过故意与活动分开来证明这一点。但是导致问题的原因是,为什么要在原件之前附加并重新创建原件?它似乎没有遵循 Android 进程的其余部分所做的逻辑生命周期。Activity
Fragment
Fragment
Activity