onDetach不需要片段吗? [英] onDetach not called for fragment?

查看:135
本文介绍了onDetach不需要片段吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了将Activity A启动到Activity B的代码.ActivityA和B都有片段实现.

I write an code to launch Activity A to Activity B. Both Activity A and B has fragment implementation.

方案:如果活动A频繁启动包含片段的活动B,那么大多数情况下它会错过Fragment.onDetach ..我用log进行检查,通常它会给我以下重写方法日志:

Scenario: If Activity A frequently launch Activity B which contain Fragment, then most of times it missed Fragment.onDetach..I checked with log, normally it give me following override method log:

  1. onAttach
  2. 列表项
  3. OnCreatView
  4. onViewCreate,然后按设备的后退"按钮
  5. onPause
  6. onStop
  7. onDestroyView
  8. onDetach

现在我按下活动B中的设备后退"按钮,它再次启动活动A,然后启动活动B并频繁重复相同的序列,然后按以下顺序更改日志序列:

now I press device Back button from Activity B which again launch Activity A then it launch Activity B and repeat same sequence frequently, then log sequence get change in following order:

  1. onAttach
  2. 列表项
  3. OnCreatView
  4. onViewCreate,然后按设备的后退"按钮
  5. onPause
  6. onStop
  7. onDestroyView并重复
  8. 没有onDetach的onAttach

有时它在onPause之后也会重复相同的行为.

some times it repeat same behaviour after onPause also.

我正在使用

getSupportFragmentManager().beginTransaction()
        .replace(R.id.fragment_container, fragment).addToBackStack(null).commitAllowingStateLoss(); to add fragment in Activity.

有什么我想念的吗..有什么建议吗?

Is there anything I am missing..any suggestion ?

推荐答案

片段被销毁后将被分离.销毁视图后,您直接完成的操作会立即取消. 请记住,销毁视图和销毁视图是片段中的两个不同的事物. 因此,请尝试onDestroyView,然后依次选择onDestroy和onDetach.

A fragment is detached after it is destroyed. what u have done is dettached directly after destroying view. Remember destroying and destroying view are two different things in fragment. So try onDestroyView, then onDestroy and then onDetach.

这篇关于onDetach不需要片段吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆