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

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

问题描述

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

解决方案

TL; DR :

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

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

太短;需要更长:

答案在原型代码本身中,

@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的文档:

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

活动达到恢复状态后,您可以自由添加和 删除活动的碎片.因此,只有在活动进行中 恢复状态可以改变片段的生命周期 独立地.

但是,当活动离开恢复状态时,该片段 活动再次将其推向生命周期.

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

警告 :如果您在Fragment中需要一个Context对象,则可以调用getActivity().但是,只有在将片段附加到活动上时,才应小心调用getActivity().如果该片段尚未附加或在其生命周期结束时分离,则getActivity()将返回null.

设计理念指出,片段是为重用而设计的. (根据设计)片段可以(并且应该)用于多个活动.

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

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

还要考虑无头碎片的情况.onAttach()为您提供初步检查所需的界面.

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:

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.

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.

Too short;Need longer:

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");
        }
    }

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

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

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.

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.

EDIT:

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

for a fragment :

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

    • onCreate()
    • onStart()
    • onResume()
    • onPause()
    • onStop()
    • onDestroy()
  2. The interaction based components:

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

from the 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.

answering another question which came up in the comments:

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.

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

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.

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

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

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

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