2017-03-31 6 views

答えて

3

メソッドがあるかどうかはわかりませんが、getContext()getActivity()の違いは、通話から返されるタイプ以外は実際にはありません。ソースからv4 Fragmentを呼び出すと、ContextActivityまたはFragmentActivityにキャストして戻す便利な方法です。これは通常の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 'を使いたい場合は' #getApplicationContext() 'を使い、アクティビティのライフサイクルに結びついた' Context'を使うなら '#getActivity()'を使います。わずかな違いですが、OPのニーズに応じて大きな影響を与えることができます。 – nbokmans

+0

あなたは正しい、私の間違い。問題は、getContext() – David

関連する問題