閱讀902 返回首頁    go 阿裏雲 go 技術社區[雲棲]


android中方便為fragment寫入參數的FragmentArgs簡介

Android開發有時候會令人頭痛。你不得不為諸如建立fragment這樣簡單的事情寫很多代碼。幸運的是java支持一個強大的工具:注釋處理器(Annotation Processors)。

Fragment的問題是你不得不設置很多參數,從而讓它正常運行。很多android開發新手通常這樣寫:

01 public class MyFragment extends Fragment
02 {
03 private int id;
04 private String title;
05  
06 public static MyFragment newInstance(int id, String title)
07 {
08 MyFragment f = new MyFragment();
09 f.id = id;
10 f.title = title;
11 return f;
12 }
13  
14 @Override
15 public View onCreateView(LayoutInflater inflater, ViewGroup container,
16 Bundle savedInstanceState)
17 {
18 Toast.makeText(getActivity(), "Hello " + title.substring(03),
19 Toast.LENGTH_SHORT).show();
20 }
21 }

這樣做怎麼了?我已經在自己的設備上嚐試過了,它很好用?

它的卻能工作,但是你有沒有試過把你的設備從豎向改為橫向?你的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。一些這樣的工作:

01 public class MyFragment extends Fragment
02 {
03 private static String KEY_ID = "key.id";
04 private static String KEY_TITLE = "key.title";
05 private int id;
06 private String title;
07  
08 public static MyFragment newInstance(int id, String title)
09 {
10 MyFragment f = new MyFragment();
11 Bundle b = new Bundle();
12 b.putInt(KEY_ID, id);
13 b.putString(KEY_TITLE, title);
14 f.setArguments(b);
15 return f;
16 }
17  
18 @Override
19 public void onCreate(Bundle savedInstanceState)
20 {
21 // onCreate it's a good point to read the arguments
22 Bundle b = getArguments();
23 this.id = b.getInt(KEY_ID);
24 this.title = b.getString(KEY_TITLE);
25 }
26  
27 @Override
28 public View onCreate(LayoutInflater inflater, ViewGroup container,
29 Bundle savedInstanceState)
30 {
31 // No NullPointer here, because onCreate() is called before this
32 Toast.makeText(getActivity(), "Hello " + title.substring(03),
33 Toast.LENGTH_SHORT).show();
34 }
35 }
我希望你現在能明白我所說的“痛苦”。在你的應用中你將會為每一個fragment寫很多代碼。如果有人為你寫這樣的代碼,這將不讓人滿意。注釋處理允許你在編譯的時候生成java代碼。注意我們並不是在討論評價在運行時間使用反射的注釋。

FragmentArgs是一個輕量的包,用於為你的fragment生成精確的java代碼。

01 import com.hannesdorfmann.fragmentargs.FragmentArgs;
02 import com.hannesdorfmann.fragmentargs.annotation.Arg;
03  
04 public class MyFragment extends Fragment
05 {
06 @Arg
07 int id;
08 @Arg
09 String title;
10  
11 @Override
12 public void onCreate(Bundle savedInstanceState)
13 {
14 super.onCreate(savedInstanceState);
15 FragmentArgs.inject(this); // read @Arg fields
16 }
17  
18 @Override
19 public View onCreateView(LayoutInflater inflater, ViewGroup container,
20 Bundle savedInstanceState)
21 {
22 Toast.makeText(getActivity(), "Hello " + title, Toast.LENGTH_SHORT)
23 .show();
24 }
25 }

隻需要在你的Fragment類中加入注釋字段,FragmentArgs就會生成引用代碼。在你的Activity中你將使用生成的Builder類(你的fragment的後綴是”Builder”),而不是使用new MyFragment()或靜態的MyFragment.newInstance(int id,String title)方法。

例如:

01 public class MyActivity extends Activity
02 {
03 public void onCreate(Bundle savedInstanceState)
04 {
05 super.onCreate(savedInstanceState);
06 int id = 123;
07 String title = "test"// Using the generated Builder
08 Fragment fragment = new MyFragmentBuilder(id, title).build(); // Fragment Transaction
09 getFragmentManager().beginTransaction().replace(R.id.container,fragment).commit();
10 }
11 }

你可能已經注意到在Fragment.onCreate(Bundle)中聲明的FragmentArgs.inject(this)。這個調用使你的fragment獲得了生成代碼的連接。你可能會問你自己:“我需不需要在每一個Fragment中的onCreate(Bundle)中加入inject()方法”。答案是no。你隻需要在你的fragment基類中插入這一句就可以,並且在所有的fragment中繼承它。

01 public class BaseFragment extends Fragment
02 {
03 @Override
04 public void onCreate(Bundle savedInstanceState)
05 {
06 super.onCreate(savedInstanceState);
07 FragmentArgs.inject(this); // read @Arg fields
08 }
09 }
10  
11 public class MyFragment extends BaseFragment
12 {
13 @Arg
14 String title;
15  
16 @Override
17 public View onCreateView(LayoutInflater inflater, ViewGroup container,
18 Bundle savedInstanceState)
19 {
20 Toast.makeText(getActivity(), "Hello " + title, Toast.LENGTH_SHORT)
21 .show();
22 }
23 }

信用:一部分的注釋生成代碼是基於Hugo Visser的Bundles項目。

最後更新:2017-05-23 15:36:24

  上一篇:go  網站建設設計前端開發需要學習html和div+css
  下一篇:go  如何從 Vim 中訪問 shell 或者運行外部命令