根据Android片段生命周期,我希望在onDestroy
之后重新创建片段,或者至少再次调用onCreateView
。
我有一个活动A为结果启动另一个活动B,活动B创建一个片段F.
public class A extends FragmentActivity {
...
public void onButonClick() {
Intent intent = new Intent(this, B.class);
startActivityForResult(intent, REQUEST_B);
}
}
public class B extends FragmentActivity {
...
public void onCreate(Bundle savedInstanceState) {
...
this.currentFragment = Fragment.instantiate(this, name);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(this.view.getFragmentContainerId(), this.currentFragment, taskName);
transaction.commit();
}
}
public class F extends Fragment {
@override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
this.view = new MyView();
}
@override
public void onResume() {
this.view.doSomething();
}
@override
public void onDestroy() {
this.view = null;
}
}
首次创建片段时,一切正常,将显示视图。离开应用程序(例如进入系统设置)会影响在没有调用onDestroy
的情况下调用片段的 onDestroyView
,但是当我回到我的应用程序时 onCreateView
不再被调用会导致NullpointerException,因为我只在onCreateView
中实例化视图。
重置onDestroyView
中的视图我认为可以解决问题,但我想知道这里的生命周期出了什么问题,如果我做错了什么。
感谢。
这是logcat输出。
03-11 11:22:47.565 6594-6594/com.xy.android.app I/ActivityA Perform button click.
03-11 11:22:47.595 6594-6594/com.xy.android.app V/ActivityA Pausing activity
03-11 11:22:47.605 6594-6594/com.xy.android.app D/ActivityB Creating activity
03-11 11:22:48.075 6594-6594/com.xy.android.app V/ActivityB Starting activity
03-11 11:22:48.105 6594-6594/com.xy.android.app I/ActivityB Resuming activity
03-11 11:22:48.476 6594-6594/com.xy.android.app I/ActivityB Starting task FragmentF.
03-11 11:22:48.536 6594-6594/com.xy.android.app I/FragmentF Attached to activity.
03-11 11:23:02.350 6594-6594/com.xy.android.app I/FragmentF Creating fragment
03-11 11:23:02.390 6594-6594/com.xy.android.app I/FragmentF Creating view for fragment
03-11 11:23:02.420 6594-6594/com.xy.android.app V/FragmentF View for fragment created
03-11 11:23:02.430 6594-6594/com.xy.android.app D/FragmentF Activity created.
03-11 11:23:02.441 6594-6594/com.xy.android.app V/FragmentF Starting fragment
03-11 11:23:02.741 6594-6594/com.xy.android.app V/ActivityA Saving activity instance state.
03-11 11:23:02.761 6594-6594/com.xy.android.app I/ActivityA Stopping activity
03-11 11:23:07.686 6594-6594/com.xy.android.app V/FragmentF Pausing fragment.
03-11 11:23:07.696 6594-6594/com.xy.android.app V/ActivityB Pausing activity
03-11 11:23:08.517 6594-6594/com.xy.android.app D/FragmentF Save instance state.
03-11 11:23:08.567 6594-6594/com.xy.android.app D/ActivityB Saving activity instance state.
03-11 11:23:08.597 6594-6594/com.xy.android.app I/FragmentF **Destroying fragment**
03-11 11:23:08.627 6594-6594/com.xy.android.app I/ActivityB Stopping activity
03-11 11:23:14.033 6594-6594/com.xy.android.app V/FragmentF Starting fragment
03-11 11:23:14.043 6594-6594/com.xy.android.app V/ActivityB Starting activity
03-11 11:23:14.063 6594-6594/com.xy.android.app I/ActivityB Resuming activity
03-11 11:23:14.063 6594-6594/com.xy.android.app I/FragmentF **Resuming fragment**
答案 0 :(得分:1)
经过一段时间的调查,我终于通过在onCreateView
中创建视图并在onDestroyView
中销毁它来“解决”了这个问题,但却没有理解为什么系统不按照sdk文档中的描述调用回调