android - 为什么在 onCreate 之前调用 onAttach?

标签 android android-fragments

在 Fragment 的生命周期中,onAttach() 方法在 onCreate() 方法之前被调用。我无法解决这个问题。为什么要先附加 Fragment?

最佳答案

TL;DR:

为了不破坏 android 中不同 UI 组件之间的设计一致性,onCreate() 方法将在所有这些组件中具有相似的功能。

当将 Container 链接到 Window 到 Activity 和 Activity 到 Fragment 等内容时,需要进行初步检查以确定容器的状态。 这就解释了 onAttach() 在 fragment 生命周期中的使用和位置。

太短了;需要更长的时间:

答案就在原型(prototype)代码本身,

@Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (OnFragmentInteractionListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

另一个例子是 Jake Wharton 的 ActionBarSherlock图书馆。

为什么要使用像 onCreate() 这样的方法,它在 activity 中具有相同的目的? , service .

onCreate() 用于处理与特定上下文创建相关的问题。如果使用 onCreate() 来检查它的容器。

我可以确定的第二个原因是 fragment 被设计为独立于 Activity 。onAttach() 提供了一个接口(interface)来确定状态/类型/(对在初始化 fragment 之前,引用 fragment 的包含 Activity 的 fragment )。

编辑:

Activity 独立存在,因此具有 self 维持的生命周期。

对于一个 fragment :

  1. 独立的生命周期组件(与任何其他组件相同):

    • onCreate()
    • onStart()
    • onResume()
    • onPause()
    • onStop()
    • onDestroy()
  2. 基于交互的组件:

    • onAttach()
    • onCreateView()
    • onActivityCreated()
    • onDestroyView()
    • onDetach()

来自 documentation :

The flow of a fragment's lifecycle, as it is affected by its host activity, (...) each successive state of the activity determines which callback methods a fragment may receive. For example, when the activity has received its onCreate() callback, a fragment in the activity receives no more than the onActivityCreated() callback.

Once the activity reaches the resumed state, you can freely add and remove fragments to the activity. Thus, only while the activity is in the resumed state can the lifecycle of a fragment change independently.

However, when the activity leaves the resumed state, the fragment again is pushed through its lifecycle by the activity.

回答评论中提出的另一个问题:

Caution: If you need a Context object within your Fragment, you can call getActivity(). However, be careful to call getActivity() only when the fragment is attached to an activity. When the fragment is not yet attached, or was detached during the end of its lifecycle, getActivity() will return null.

设计理念表明 fragment 是为重用而设计的。一个 fragment (根据设计)可以(并且应该)在多个 Activity 中使用。

onCreate 根据定义负责创建 fragment 。 考虑方向的情况,您的 fragment 可能是: - 在不同的方向使用不同的布局。 - 仅适用于纵向而不是横向 - 只能在 table 和手机上使用。

所有这些情况都需要在从 android 透视图 (onCreate()) 和 View 膨胀 (onCreateView()) 初始化 fragment 之前进行检查。

还要考虑 headless fragment 的情况.onAttach()为您提供初步检查所需的接口(interface)。

关于android - 为什么在 onCreate 之前调用 onAttach?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29677812/

相关文章:

android - 根据触摸事件更改 View

android - android(eclipse)中的不满意链接错误

Android Fragment onAttach() 已弃用

java - Android NullPointerException 蓝牙

java - 如何防止 editText 与其他 View 重叠?

android - 以编程方式更改键盘语言

java - 在 Android Instrumentation 测试中使用 PowerMock 和 Mockito - 错误 - 重复文件 - org.mockito.plugins.MockMaker

android - 带嵌套 fragment 的抽屉导航 (ViewPager)

java - findFragmentByTag() 返回 null

android - 如何在 Fragment 中为微调器设置 ArrayAdapter