天天看点

android中方便为fragment写入参数的FragmentArgs简介

android开发有时候会令人头痛。你不得不为诸如建立fragment这样简单的事情写很多代码。幸运的是java支持一个强大的工具:注释处理器(annotation processors)。

fragment的问题是你不得不设置很多参数,从而让它正常运行。很多android开发新手通常这样写:

<code>01</code>

<code>public</code> <code>class</code> <code>myfragment </code><code>extends</code> <code>fragment</code>

<code>02</code>

<code>{</code>

<code>03</code>

<code>private</code> <code>int</code> <code>id;</code>

<code>04</code>

<code>private</code> <code>string title;</code>

<code>05</code>

<code>06</code>

<code>public</code> <code>static</code> <code>myfragment newinstance(</code><code>int</code> <code>id, string title)</code>

<code>07</code>

<code>08</code>

<code>myfragment f = </code><code>new</code> <code>myfragment();</code>

<code>09</code>

<code>f.id = id;</code>

<code>10</code>

<code>f.title = title;</code>

<code>11</code>

<code>return</code> <code>f;</code>

<code>12</code>

<code>}</code>

<code>13</code>

<code>14</code>

<code>@override</code>

<code>15</code>

<code>public</code> <code>view oncreateview(layoutinflater inflater, viewgroup container,</code>

<code>16</code>

<code>bundle savedinstancestate)</code>

<code>17</code>

<code>18</code>

<code>toast.maketext(getactivity(), </code><code>"hello "</code> <code>+ title.substring(</code><code>0</code><code>, </code><code>3</code><code>),</code>

<code>19</code>

<code>toast.length_short).show();</code>

<code>20</code>

<code>21</code>

这样做怎么了?我已经在自己的设备上尝试过了,它很好用?

它的却能工作,但是你有没有试过把你的设备从竖向改为横向?你的app将会因为nullpointerexception而崩溃,当你试图访问id或title时。

我的app是正常的,因为我把app设置为竖向。所以我从来没遇到过这个问题。

随便你!android是一个真正的多任务操作系统。多个app在同一时间运行,同时如果需要内存android系统将会销毁activity(和其中包含的fragment)。可能你在日常的app开发中不会注意这些问题。然而,当你在play store中发布后,你将会注意到你的app崩溃了,但你不知道什么原因。使用你app的用户可能同时间使用多个app,很有可能你的app在后台被销毁了。例如:a 用户打开你的app,myfragment在屏幕上显示。下一步你的用户按了home键(这是你的app在后台运行),并且打开了其它应用。你的app可能会因为释放内存而被销毁。之后,用户返回你的app,例如通过多任务按钮。所以,android现在会怎么做?android会恢复之前的app状态,同时恢复myfragment,这就是问题所在。fragment试图访问title,但title是null,因为它不是被永久保存的。

我知道了,所以我需要把它们保存在onsaveinstancestate(bundle)中?

不是。官方的文档有一些不清楚,但是onsaveinstancestate(bundle)的使用方法应该跟你用activity.onsaveinstancestate(bundle)一样:你使用这个方法保存实例的“临时”状态,例如去处理屏幕的方向(从竖向到横向,反之亦然)。所以说当app在后台被杀掉时fragment的实例状态并不能被保存成持久数据,它的作用是再一次返回前台时恢复数据。它的作用跟activity.onsaveinstancestate(bundle)在activity中的作用相同,它们用于“临时”保存实例状态。然而,持久的参数是通过intent外部数据传输的。

所以我应该在activity中得intent保存fragment的参数?

不需要,fragment有它自己的机制。有两个方法:fragment.setarguments(bundle)和fragment.getarguments(),你必须通过这两个方法来确保参数被持久保存。这就是我上面提到的痛苦之处。需要有大量的代码这样写。第一,你要创建一个bundle,然后你需要放入键值对,最后调用fragment.setarguments()。不幸的是,你的工作还没有结束,你必须通过fragment.getarguments()来读出bundle。一些这样的工作:

<code>private</code> <code>static</code> <code>string key_id = </code><code>"key.id"</code><code>;</code>

<code>private</code> <code>static</code> <code>string key_title = </code><code>"key.title"</code><code>;</code>

<code>bundle b = </code><code>new</code> <code>bundle();</code>

<code>b.putint(key_id, id);</code>

<code>b.putstring(key_title, title);</code>

<code>f.setarguments(b);</code>

<code>public</code> <code>void</code> <code>oncreate(bundle savedinstancestate)</code>

<code>// oncreate it's a good point to read the arguments</code>

<code>22</code>

<code>bundle b = getarguments();</code>

<code>23</code>

<code>this</code><code>.id = b.getint(key_id);</code>

<code>24</code>

<code>this</code><code>.title = b.getstring(key_title);</code>

<code>25</code>

<code>26</code>

<code>27</code>

<code>28</code>

<code>public</code> <code>view oncreate(layoutinflater inflater, viewgroup container,</code>

<code>29</code>

<code>30</code>

<code>31</code>

<code>// no nullpointer here, because oncreate() is called before this</code>

<code>32</code>

<code>33</code>

<code>34</code>

<code>35</code>

我希望你现在能明白我所说的“痛苦”。在你的应用中你将会为每一个fragment写很多代码。如果有人为你写这样的代码,这将不让人满意。注释处理允许你在编译的时候生成java代码。注意我们并不是在讨论评价在运行时间使用反射的注释。

fragmentargs是一个轻量的包,用于为你的fragment生成精确的java代码。

<code>import</code> <code>com.hannesdorfmann.fragmentargs.fragmentargs;</code>

<code>import</code> <code>com.hannesdorfmann.fragmentargs.annotation.arg;</code>

<code>@arg</code>

<code>int</code> <code>id;</code>

<code>string title;</code>

<code>super</code><code>.oncreate(savedinstancestate);</code>

<code>fragmentargs.inject(</code><code>this</code><code>); </code><code>// read @arg fields</code>

<code>toast.maketext(getactivity(), </code><code>"hello "</code> <code>+ title, toast.length_short)</code>

<code>.show();</code>

只需要在你的fragment类中加入注释字段,fragmentargs就会生成引用代码。在你的activity中你将使用生成的builder类(你的fragment的后缀是”builder”),而不是使用new myfragment()或静态的myfragment.newinstance(int id,string title)方法。

例如:

<code>public</code> <code>class</code> <code>myactivity </code><code>extends</code> <code>activity</code>

<code>int</code> <code>id = </code><code>123</code><code>;</code>

<code>string title = </code><code>"test"</code><code>; </code><code>// using the generated builder</code>

<code>fragment fragment = </code><code>new</code> <code>myfragmentbuilder(id, title).build(); </code><code>// fragment transaction</code>

<code>getfragmentmanager().begintransaction().replace(r.id.container,fragment).commit();</code>

你可能已经注意到在fragment.oncreate(bundle)中声明的fragmentargs.inject(this)。这个调用使你的fragment获得了生成代码的连接。你可能会问你自己:“我需不需要在每一个fragment中的oncreate(bundle)中加入inject()方法”。答案是no。你只需要在你的fragment基类中插入这一句就可以,并且在所有的fragment中继承它。

<code>public</code> <code>class</code> <code>basefragment </code><code>extends</code> <code>fragment</code>

<code>public</code> <code>class</code> <code>myfragment </code><code>extends</code> <code>basefragment</code>

信用:一部分的注释生成代码是基于hugo visser的bundles项目。

继续阅读