Android的碎片onAttach()去precated [英] Android Fragment onAttach() deprecated

查看:404
本文介绍了Android的碎片onAttach()去precated的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经更新了我的应用程序使用了最新的支持库(23.0.0版),我发现了,他们去precated的片段类的onAttach()函数。

I have updated my app to use the latest support library (version 23.0.0), I've found out that they deprecated the onAttach() function of the Fragment class.

相反的:

onAttach (Activity activity)

这是现在的:

It's now:

onAttach (Context context)

由于我的应用程序采用前代precation通过活动,一个可能的解决办法,我认为是:

As my app uses the activity passed before deprecation, a possible solution i think is:

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    activity = getActivity();
}

请问这是否做了正确的方法是什么?

Would that be the correct way to do it?

更新:

如果我运行与API的设备超过23时,新onAttach()甚至没有被调用。我希望这不是他们打算做!

If i run a device with API lower than 23, the new onAttach() is not even being called. I hope that this is not what they intended to do!

更新2:

问题已得到解决的最新更新的SDK。

Issue has been resolved with the latest updates to the SDK.

我已经测试了我的API 22设备和onAttach(上下文)被调用。

I have tested on my API 22 device and onAttach(Context) is being called.

点击这里跟着我开了一个bug报告前几个星期,从球员的答案在谷歌。

Click here to follow the bug report I've opened a couple of weeks ago and the answers from the guys at Google.

推荐答案

活动是一个方面,所以如果你可以简单地检查方面是一种活动,如有必要,将它转换。

Activity is a context so if you can simply check the context is an Activity and cast it if necessary.

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

    Activity a;

    if (context instanceof Activity){
        a=(Activity) context;
    }

}

更新:有些人声称,新的上下文覆盖永远不会被调用。我做了一些测试,并不能找到这样一个场景,这是真实的,并根据源$ C ​​$ C,它不应该是真实的。在所有的情况下,我测试过,无论是pre和后SDK23,无论是活动上下文版本 onAttach 被称为。如果你能找到这样一个场景,这种情况并非如此,我建议你创建出的问题和示例项目的报告给Android团队

Update: Some are claiming that the new Context override is never called. I have done some tests and cannot find a scenario where this is true and according to the source code, it should never be true. In all cases I tested, both pre and post SDK23, both the Activity and the Context versions of onAttach were called. If you can find a scenario where this is not the case, I would suggest you create a sample project illustrating the issue and report it to the Android team.

更新2:我只使用Android支持库片段的错误得到修复更快出现。看来上面的问题,即覆盖没有得到正确调用只涉及到光,如果你使用的框架片段。

Update 2: I only ever use the Android Support Library fragments as bugs get fixed faster there. It seems the above issue where the overrides do not get called correctly only comes to light if you use the framework fragments.

这篇关于Android的碎片onAttach()去precated的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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