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文檔中描述的那樣調用回調。

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

  1. android view禁用,Android 禁止ViewPager的自带滑动效果

    由于最近在做墨水屏的相关工作,ViewPager自带的滑动效果在墨水屏上表现的很不好,残影太重了.所以禁止自带的滑动效果,用接口来接管相关逻辑实现自处理. import android.content ...

  2. Android view变形,android仿变形金刚效果实现MatchView

    what?变形金刚!先看效果吧! MainActivity.javapackage com.lee.matchview; import com.example.matchview.R; import ...

  3. Android view.settran,Android RecyclerView从入门到玩坏

    目录 前言 基础使用 分隔线 点击监听 搭配CardView 更丰富的条目 增删条目 快速添加视图 让RecyclerView支持复杂视图 最后前言 RecyclerView在Android界面开发当 ...

  4. android view嵌套,Android RecyclerView嵌套RecyclerView点击事件

    RecyclerView嵌套RecyclerView点击事件遇到取值错乱的问题 其实问题根本就是咱们没有处理好被嵌套的那层recycleview的position 那么解决思路也很简单,想办法把被嵌套 ...

  5. android.view.surface,Android SurfaceView 源码分析及使用

    概述 SurfaceView 是 Android 中一种比较特殊的视图(View),它跟平时时候的 TextView.Button 最大的区别是它跟它的视图容器并不是在同一个视图层上,它的 UI 显示 ...

  6. android view flipper,Android之ViewFlipper的简单使用

    大家都使用过ViewPager,但是ViewPager还有一个兄弟,那就是ViewFlipper.两者的名字非常相似,我们可以将ViewPager理解成"一页一页的视图",View ...

  7. android view stop,android – onDestroyView片段永远不会在onStop之后调用

    所以你的问题似乎是"当你继续向堆栈应用程序添加太多活动时会逐渐抛出OOM异常",并且你认为原因可能是切换活动时顶层Fragment上没有调用onDestroyView(). OnD ...

  8. android view setleft,android – 在新textview上使用setLeft / setRight方法

    我正在以编程方式使用TextViews,我需要动态添加新视图并在RelativeLayout父级中设置它们的左/顶位置. 我正在做的是这样的: RelativeLayout global=(Relat ...

  9. android view 平滑,Android移动view动画问题(让移动更平滑)

    Android写动画效果不是一般的麻烦,网上找了好久,终于解决了动画的问题,总结记录以共勉. 仅以水平方向移动效果做说明,垂直方向类似. 完整动画函数代码: public void slideview ...

最新文章

  1. 从观望到行动:全球工业物联网市场生态全景观察
  2. 马蜂窝事件背后暴露出的数据风险
  3. Linux管道编程实例
  4. 华为鸿蒙电脑操作系统测试版,官方公布教程:鸿蒙OS测试版返回EMUI 11
  5. linux打包运行python文件_Linux下安装pyinstaller用于将py文件打包生成一个可执行文件...
  6. NHibernate快速起步
  7. CF 1093G Multidimensional Queries——线段树(消去绝对值符号)
  8. oracle闪回特性,oracle闪回特性
  9. JS高级——手写call()、apply()、bind()
  10. 面试必备:Spring 面试 63 问!
  11. 1.5 编程基础之循环控制 45 金币 方法二(python3实现)
  12. c#获取网络时间并同步本地时间
  13. 如何使用3klCon实现大规模自动化网络侦察
  14. Spring 注解中@Resource 和 Authwired 的区别
  15. 【编程开发】之国际号码校验工具 Libphonenumber
  16. Camunda流程引擎事件(Events)介绍
  17. 【Python进阶】9- Pandas的应用
  18. 单片机程序跑飞的原因
  19. 跳转饿了么小程序、跳转美团小程序、获取小程序路径、小程序生成小程序码不能选择第三方小程序、领红包功能开发(附源码)
  20. linux学习(一)虚拟机界面全屏

热门文章

  1. Keruskal 算法
  2. MySQL 5.7配置教程
  3. linux+软盘启动程序,制作在软盘上跑的linux引导器详细过程_linux教程
  4. 我已经迫不及待想与你分享他的“肉体”
  5. 利用scrapy爬取京东移动端的图片素材和商品信息
  6. 4、调用WebService查询手机号码归宿地
  7. LeetCode 笔记系列16.1 Minimum Window Substring [从O(N*M), O(NlogM)到O(N),人生就是一场不停的战斗]...
  8. vue-video-player记录上次播放时间继续播放
  9. Intel Aero飞行日记
  10. 3d max 网络渲染