Android 支持库 27、Fragment 更新? [英] Android support library 27, Fragment update?

查看:25
本文介绍了Android 支持库 27、Fragment 更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自从我将我的项目更新到 SDK 版本 27 并将支持库的 gradle 插件更新到版本 27.0.0 后,我需要更改我的代码.

Since I updated my project to SDK version 27 and gradle plugins for the support library to version 27.0.0 I needed to change my code.

使用 26.1.0 我可以在我的 Fragment 中使用 getContext()(使用 Kotlin context)(android.support.v4.app) 并且我没有可空性问题,但是由于我使用 Kotlin,我在版本 27.0.0 上遇到了问题,我所有的 context 调用不再起作用,我需要一个安全操作符,比如 context!!,但是因为我个人发现每次我自己做这件事时都会很忙,所以我解决了功能

With 26.1.0 I can just use getContext() (with Kotlin context) in my Fragment (android.support.v4.app) and I have no nullability issues, but since I use Kotlin I have a problem with version 27.0.0, all my context calls did not work anymore, I needed a safety operator, like context!!, but since I personally find it to be a hustle to do that every time I just made myself I workaround function

override fun getContext() = super.getContext()!!

另一件改变的事情(突然,这就是我问的原因)是方法 onCreateView()onViewCreated().在 onCreateView 中,inflater 不再可能为 null,所以我需要更改我的函数签名以正确覆盖从 onCreateView(inflater: LayoutInflater?...)onCreateView(inflater: LayoutInflater...)onViewCreated 中的 createdView 参数相同.

Another thing that change (suddenly and that is why I am asking) are the methods onCreateView() and onViewCreated(). In onCreateView the inflater is not possibly null anymore, so I needed to change my function signature to properly override from onCreateView(inflater: LayoutInflater?...) to onCreateView(inflater: LayoutInflater...) and the same for the createdView parameter in onViewCreated.

所以现在我想知道为什么,尤其是(对于 Kotlin)非常丑陋的 getContext() 更改并转到 https://developer.android.com/sdk/support_api_diff/27.0.0/changes.html.

So now I was wondering why, especially the (for Kotlin) very ugly getContext() change was made and headed over to https://developer.android.com/sdk/support_api_diff/27.0.0/changes.html.

但是等等,显然他们没有改变它?所以现在我的问题是我是否做错了什么,或者他们是否真的改变了它,如果是这样,我可能会问他们为什么?

But wait, apparently they did not change it? So now my question is if I am doing something wrong or if they really changed it and if so I might ask them why?

顺便说一下,同样适用于 getActivity(),我认为添加了 mHost == null 检查并且 getActivity 方法是即使是最终的,所以我不能在那里使用我的解决方法,这使得它非常非常难看.实际上在源文件中方法看起来是一样的,但是 26.1.0 有 Kotlin 返回类型 Context!27.0.0 返回类型 <代码>上下文?.

By the way, same applies for getActivity(), I think the mHost == null check was added and the getActivity method is even final, so I cannot use my workaround there, which makes it very very ugly. Actually in the source files the methods look like the same, but 26.1.0 has Kotlin return type Context! and 27.0.0 return type Context?.

推荐答案

这些是经过深思熟虑的更改.在这个版本的支持库之前,这些类没有可空性注释,所以从 Kotlin 开始,所有这些类型都只是 平台类型.在 27 中,他们添加了必要的注解,所以现在这些类型在 Kotlin 中明确标记为可空或不可空——无需猜测它们是否可以 null.

These were deliberate changes. Before this version of the support library, these classes had no nullability annotations, so from Kotlin, all these types were just platform types. In 27, they added the necessary annotations, so now these types are definitely marked as either nullable or non-nullable in Kotlin - there's no need to guess whether they can be null.

至于你提到的具体方法:

As for the specific methods you've mentioned:

  • getActivitygetContext 方法返回可空类型,因为当 Fragment 未附加到 Activity 时,这些方法已经返回 null.行为没有变化,只是现在被明确标记,因此您可以安全地处理它.
  • onCreateView 方法的 inflater 参数曾经是一个平台类型,因此是否将其标记为可空取决于您.由于它永远不会被 null 调用,所以它被显式标注为 @NonNull,所以它在 Kotlin 中的类型现在严格为 LayoutInflater 而不是松散的"LayoutInflater! 类型.
  • The getActivity and getContext methods return nullable types because when the Fragment is not attached to an Activity, these methods already returned null. There's no change in behaviour, it's just explicitly marked now, so you can safely handle it.
  • The inflater parameter of the onCreateView method used to be a platform type, so it was up to you whether you marked it nullable or not. Since it will never be called with null, it has been explicitly annotated as @NonNull, so its type in Kotlin now is strictly LayoutInflater instead of the "looser" LayoutInflater! type.

从支持库 27.1.0 开始,您可以使用 requireActivityrequireContext 方法,这些方法返回不可为空的类型,但需要注意的是,当使用常规方法时,它们会抛出 IllegalStateException将返回 null.

starting from support library 27.1.0, you can use the requireActivity and requireContext methods, which return non-nullable types, with the caveat that they'll throw an IllegalStateException when the regular methods would return null.

这篇关于Android 支持库 27、Fragment 更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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