What's the point of setArguments?

前端 未结 4 1659
忘了有多久
忘了有多久 2020-12-13 03:55

Hi I was looking at the following Fragments example on the android site.

http://developer.android.com/guide/components/fragments.html#Example

I would like to

4条回答
  •  北荒
    北荒 (楼主)
    2020-12-13 04:37

    You can use getters and setters, but by passing in a bundle you don't need to write that code, since it's already there. Also, I believe that these arguments are automatically passed in again if the screen orientation changes, which also makes life easier.

    Essentially, setArguments and getArguments is just a design pattern that Google suggests you follow:

    Every fragment must have an empty constructor, so it can be instantiated when restoring its activity's state. It is strongly recommended that subclasses do not have other constructors with parameters, since these constructors will not be called when the fragment is re-instantiated; instead, arguments can be supplied by the caller with setArguments(Bundle) and later retrieved by the Fragment with getArguments(). http://developer.android.com/reference/android/app/Fragment.html

    I take that to include setters which are needed for your Fragment to operate as well. Then again - there's nothing forcing you to do it this way, and as you know - it's not the only way things could be made to work.

提交回复
热议问题