2017-05-14 18 views

回答

4

isAdded()是更好地使用幾乎所有的情況下,這些原因有二:

  1. isAdded()返回true,如果該片段目前添加到其 活動。 getActivity()只是返回相關的活動。在大多數情況下,這將返回相同的布爾,但更好的是安全

  2. 這是更少的代碼編寫

的源代碼:

http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/android/app/Fragment.java/

/** 
* Return the Activity this fragment is currently associated with. 
*/ 
final public Activity getActivity() { 
    return mActivity; 
} 

/** 
* Return true if the fragment is currently added to its activity. 
*/ 
final public boolean isAdded() { 
    return mActivity != null && mAdded; 
} 
+1

的isAdded節目實施毫無疑問,他們是不一樣的。 –

相關問題