android view ondestroy,Android片段onCreateView在onDestroy之后不再調用。

According to the Android fragment lifecycle i would expect that after onDestroy the fragment will be recreated, or at least onCreateView is called again.

根據Android片段生命周期,我希望在onDestroy之后重新創建片段,或者至少調用onCreateView。

I have an Activity A starting another Activity B for result and Activity B creating a fragment F.

我有一個活動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;

}

}

When the Fragment is created first time everything is ok, the view is shown. Leaving the app (such as going to system settings) has the affect that onDestroy of the fragment is called without onDestroyView is being called, but when i go back to my app onCreateView ist not called again which causes a NullpointerException because i am instantiating the view only in onCreateView. Resetting the view in onDestroyView i think would solve the problem, but i want to know what's going wrong here with the lifecycyle and if i am doing something wrong.

當第一次創建片段時,一切正常,將顯示視圖。離開應用程序(比如進入系統設置)會影響到調用onDestroy來調用片段,而沒有調用on驅逐艦視圖,但是當我返回到我的app onCreateView ist時不會再調用它,這會導致NullpointerException,因為我只在onCreateView中實例化視圖。在on驅逐艦視圖中重新設置視圖,我認為可以解決這個問題,但是我想知道生命周期中發生了什么問題,如果我做錯了什么事情。

Thanks.

謝謝。

Here is the logcat output.

這是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**

1 个解决方案

#1

1

After investigating some time i finally "solved" the problem by creating the view in onCreateView and destroy it in onDestroyView, without understanding why the system does not call the callback as described in the sdk documentation.

在調查了一段時間之后,我終於通過在onCreateView中創建視圖並在ondestroy視圖中銷毀它來“解決”了這個問題,而不理解為什么系統不像sdk文檔中描述的那樣調用回調。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值