gpt4 book ai didi

android - 如果在 fragment 代码中没有覆盖 onAttach() 怎么办?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:12:24 24 4
gpt4 key购买 nike

根据 fragment 生命周期,onAttach()onCreate() 之前被调用,因此它将托管 Activity 分配给 fragment 。所以,我想知道如果它没有被覆盖会怎样。所有 fragment 回调的默认定义是否已经存在?

最佳答案

来自documentation :

void onAttach (Activity activity)

called once the fragment is associated with its activity. This method was deprecated in API level 23. Use onAttach(Context) instead.

If you override this method you must call through to the superclass implementation.

void onAttach (Context context)

Called when a fragment is first attached to its context. onCreate(Bundle) will be called after this.

这是 fragment 的生命周期设计。不重写该方法就没有问题。

所有 fragment 回调的默认定义是否已经存在?

不行,你需要自己创建 fragment 回调。 onAttach() 方法通常被覆盖以确保 fragment 的父 Activity 正在实现 fragment 回调。像这样的东西(在 Communicating with Other Fragments 阅读更多内容):

public class HeadlinesFragment extends ListFragment {
OnHeadlineSelectedListener mCallback;

// Container Activity must implement this interface
public interface OnHeadlineSelectedListener {
public void onArticleSelected(int position);
}

@Override
public void onAttach(Context context) {
super.onAttach(context);

// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
mCallback = (OnHeadlineSelectedListener) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString()
+ " must implement OnHeadlineSelectedListener");
}
}

...
}

当父 Activity 未实现 OnHeadlineSelectedListener 时,应用程序将崩溃并抛出 必须实现 OnHeadlineSelectedListener。因此,它将防止您在代码中引入逻辑错误。


更新

onAttach() 的目的是什么?

According to fragment lifecycle onAttach() is called before onCreate() so that it assigns hosting activity to the fragment.

这到底是什么意思?

简单的回答:这是 Fragment 的生命周期,我们可以在其中知道 Fragment 何时附加到它的父 Activity。

更多详情:

来自以下source code of onAttach() :

/**
* Called when a fragment is first attached to its context.
* {@link #onCreate(Bundle)} will be called after this.
*/
@CallSuper
public void onAttach(Context context) {
mCalled = true;
final Activity hostActivity = mHost == null ? null : mHost.getActivity();
if (hostActivity != null) {
mCalled = false;
onAttach(hostActivity);
}
}

/**
* @deprecated Use {@link #onAttach(Context)} instead.
*/
@Deprecated
@CallSuper
public void onAttach(Activity activity) {
mCalled = true;

}

除了关于我们上一个问题的文档,我们什么也看不到mHost.

关于 Fragment 的源代码 https://github.com/android/platform_frameworks_base/blob/master/core/java/android/app/Fragment.java#L435 ,我们可以知道mhost其实是一个FragmentHostCallback:

// Activity this fragment is attached to.
FragmentHostCallback mHost;

但是如果我们扫描所有的源代码 fragment ,我们将无法获得任何初始化mhost的线索。

从Fragment生命周期图中我们知道,添加Fragment时生命周期是开始的:

enter image description here

以编程方式,我们添加 fragment :

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();

检查 FragmentManager source code at line 1200 to 1229来自方法 moveToState():

void moveToState(Fragment f, int newState, int transit, int transitionStyle,
boolean keepActive) {
}

我们有以下代码:

f.mHost = mHost;
f.mParentFragment = mParent;
f.mFragmentManager = mParent != null
? mParent.mChildFragmentManager : mHost.getFragmentManagerImpl();

// If we have a target fragment, push it along to at least CREATED
// so that this one can rely on it as an initialized dependency.
if (f.mTarget != null) {
if (mActive.get(f.mTarget.mIndex) != f.mTarget) {
throw new IllegalStateException("Fragment " + f
+ " declared target fragment " + f.mTarget
+ " that does not belong to this FragmentManager!");
}
if (f.mTarget.mState < Fragment.CREATED) {
moveToState(f.mTarget, Fragment.CREATED, 0, 0, true);
}
}

dispatchOnFragmentPreAttached(f, mHost.getContext(), false);
f.mCalled = false;
f.onAttach(mHost.getContext());
if (!f.mCalled) {
throw new SuperNotCalledException("Fragment " + f
+ " did not call through to super.onAttach()");
}
if (f.mParentFragment == null) {
mHost.onAttachFragment(f);
} else {
f.mParentFragment.onAttachFragment(f);
}

现在我们知道Fragment的mHostonAttach()是被FragmentManager初始化和调用的。

关于android - 如果在 fragment 代码中没有覆盖 onAttach() 怎么办?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46336201/

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