gpt4 book ai didi

android - Fragment 和父 Activity 一起生命周期

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:06:12 25 4
gpt4 key购买 nike

我正在关注这四个主题 Creating a Fragment , Handling the Fragment Lifecycle , Managing the Activity LifecyclePausing and Resuming an Activity .所以我对此有点怀疑。我的问题是

<醇>
  • 如果 A Activity 通过 Intent 调用 B Activity 但 A 没有调用 finish() 方法,那么 A如果 B 是 TransparentSemiTransparent,将处于 Pause 状态;如果 B 是 Opaque,则将处于 Stop 状态。我说得对吗?
  • 如果 A Activity 包含 Fragment F 那么如果 A 将进入 Pause 状态那么 F 将进入 Pause 状态,如果 A 将处于 Stop 状态,则 F 也将处于 Stop 状态。我说得对吗?
  • 如果 A 调用 B Activity 并且 B 是 Transparent,那么 A 将处于 Pause 状态,F 也会。如果 B 调用 finish(),那么 A 将进入 Resume 状态,但 F 会怎样?它会从暂停中恢复吗?如果它是那么如何以及采取什么步骤因为我没有看到 Fragment 生命周期中的任何直接链接指示 onPause()onResume()直接作为 Activity 可以做的。

    Hope I am able to ask what I want. Sorry for my bad Englsh.

  • 最佳答案

    1. 据我所知,如果 B 是半透明的或部分可见的,您不能确定仅对 A 调用 onPause:

      Paused

      Another activity is in the foreground and has focus, but this one is still visible. That is, another activity is visible on top of this one and that activity is partially transparent or doesn't cover the entire screen. A paused activity is completely alive (the Activity object is retained in memory, it maintains all state and member information, and remains attached to the window manager), but can be killed by the system in extremely low memory situations.

    2. 是的,你是对的:

      The lifecycle of the activity in which the fragment lives directly affects the lifecycle of the fragment, such that each lifecycle callback for the activity results in a similar callback for each fragment. For example, when the activity receives onPause(), each fragment in the activity receives onPause().

      然而,反之则不然,这意味着如果 fragment 接收到 onStop,则不能保证会调用 Activity 的 onStop。

    3. 我不太确定您最后一句话的意思或您如何测试它。根据 fragment 文档:

      public void onResume ()

      Called when the fragment is visible to the user and actively running. This is generally tied to Activity.onResume of the containing Activity's lifecycle

      之所以说一般,是因为它取决于 Activity 如何处理 fragment 。

    关于android - Fragment 和父 Activity 一起生命周期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12833854/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com