简体   繁体   中英

When retrieving passed arguments from a bundle, why does Android Studio warn and suggest using requireArguments() instead of arguments!!?

So, like the title says I'm trying to retrieve passed arguments from a bundle according to the documentation I'm following by using var args = GameWonFragmentArgs.fromBundle(arguments!!) . But Android Studio suggests using the following code: var args = GameWonFragmentArgs.fromBundle(requireArguments())

I looked up the Android Documentation for Fragment and found that requireArguments() returns a @NonNull Bundle or an IllegalStateException. Why does Android Studio suggest using it and what's the difference between the two?

Also, I couldn't find relevant documentation about fromBundle() .

There is some api changes in androidx.fragment:fragment

requireArguments() --- method which returns a @NonNull Bundle or throws an IllegalStateException.

fromBundle(arguments!!) --- these case App may be crashed because of Null pointer exception. if arguments is null app will be crashed.

As per the UseRequireInsteadOfGet Lint warning:

AndroidX added new "require____()" versions of common "get___()" APIs, such as getContext/getActivity/getArguments/etc. Rather than wrap these in something like requireNotNull(), using these APIs will allow the underlying component to try to tell you why it was null, and thus yield a better error message.

Importantly, the error messages always mention which fragment caused the issue, including the class name of the Fragment and its unique ID. This can be particularly useful when correlating that ID with Fragment's debug logging , thus allowing you to understand when that Fragment was first added or updated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM