为什么 onAttach 在 onCreate 之前被调用? [英] Why is onAttach called before onCreate?

查看:39
本文介绍了为什么 onAttach 在 onCreate 之前被调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In a Fragment's Lifecycle, the onAttach() method is called before the onCreate() method. I can't wrap my head around this. Why would you attach a Fragment first?

推荐答案

TL;DR:

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

In order to not break the design consistency amongst different UI components in android,the onCreate() method will have similar functionality across all of them.

将容器链接到诸如窗口到活动和活动到片段之类的内容时,需要进行初步检查以确定容器的状态.这解释了 onAttach() 在片段生命周期中的用途和位置.

When linking Containers to Contents like Window to Activity and Activity to Fragment a preliminary check needs to be done to determine the state of container. And that explains the use and position of onAttach() in the fragment lifecycle.

太短;需要更长的时间:

答案在于原型代码本身,

The answer is in the archetype code itself,

@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 库.

Another example would be in Jake Wharton's ActionBarSherlock library.

为什么要使用 onCreate() 之类的方法,它在 activity ,服务.

Why would you want to use a method like onCreate() which is has the same purpose in an activity ,service.

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

The onCreate() is meant to handle issues with respect to that particular context creation.It does not make sense if onCreate() is used to check the state of its container.

我可以确定的第二个原因是片段被设计为独立于活动.onAttach() 提供了一个接口来确定状态/类型/(其他与片段)在初始化片段之前参考片段.

The second reason that I can come determine is that a fragment is designed to be activity independent.The onAttach() provides an interface to determine the state/type/(other detail that matters to the fragment) of the containing activity with reference to the fragment before you initialize a fragment.

编辑:

一项活动独立存在,因此具有自我维持的生命周期.

An activity exists independently and therefore has a self sustaining lifecycle.

对于片段:

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

  1. The independent lifecycle components(same as any other components):

  • onCreate()
  • onStart()
  • onResume()
  • onPause()
  • onStop()
  • onDestroy()

基于交互的组件:

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

来自文档:

片段生命周期的流程,因为它受其宿主的影响活动,(...)活动的每个连续状态决定了哪个片段可能接收的回调方法.例如,当活动已收到其 onCreate() 回调,这是活动只接收 onActivityCreated() 回调.

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.

但是,当活动离开恢复状态时,片段再次被 Activity 推送通过其生命周期.

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

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

answering another question which came up in the comments:

注意:如果您的 Fragment 中需要一个 Context 对象,您可以调用 getActivity().但是,仅当片段附加到活动时才小心调用 getActivity().当片段尚未附加或在其生命周期结束时被分离时,getActivity() 将返回 null.

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 是为重用而设计的.一个片段(按设计)可以(并且应该)在多个活动中使用.

The design philosophy states that a Fragment is designed for reuse. A fragment (by design) could(and should) be used across multiple activities.

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

The onCreate by definition is responsible to create a fragment. Consider the case of orientation,your fragment could be: - using different layouts in different orientations. - applicable only in portrait orientation and not landscape - To be used only on tables and mobile phones.

所有这些情况都需要在从 android 角度(onCreate())和视图膨胀(onCreateView())初始化片段之前进行检查.

All these situations would require a check before the fragment is initialized from the android perspective(onCreate()) and the view inflated(onCreateView()).

还要考虑一个无头片段的情况.onAttach() 为您提供初步检查所需的接口.

Also consider the situation of a headless fragment.The onAttach() provides you the interface required for preliminary checks.

这篇关于为什么 onAttach 在 onCreate 之前被调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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