2017-03-31 91 views

回答

3

不確定是否有getActivityContext()方法,但只要getContext()getActivity()之間的差異,確實沒有比您從呼叫回來的類型。從源代碼v4碎片,它只是一個便利的方法,在返回之前將Context轉換爲ActivityFragmentActivity,對於正常的Fragment類,它類似。

/** 
* Return the {@link Context} this fragment is currently associated with. 
*/ 
public Context getContext() { 
    return mHost == null ? null : mHost.getContext(); 
} 

/** 
* Return the {@link FragmentActivity} this fragment is currently associated with. 
* May return {@code null} if the fragment is associated with a {@link Context} 
* instead. 
*/ 
final public FragmentActivity getActivity() { 
    return mHost == null ? null : (FragmentActivity) mHost.getActivity(); 
} 

此外,活動是一個上下文,所以沒有太大的區別。

+0

我想OP是在談論'Activity#getApplicationContext()' - 在這種情況下,實際上存在着不小的差異。如果你想要一個綁定到整個應用程序生命週期的'Context',或者如果你想要一個與該活動生命週期相關的'Context',可以使用'#getActivity()'來使用'#getApplicationContext()'。細微的差異,但取決於OP的需求會產生巨大的影響。 – nbokmans

+0

你說得對,我的錯。問題是關於getContext() – David

相關問題