Android: Fragment 的新getContext() 方法是哪个上下文?
Android: Fragment's new getContext() method is which context?
Fragment.getContext()
的文档说了
returns the context the Fragment is currently associated with.
它是在 api 23 年引入的
http://developer.android.com/reference/android/app/Fragment.html#getContext()
这是Application
还是Activity
Context
?
至于 FragmentActivity 和继承 - 'getContext()' 仍然会 return activity 上下文,如果你检查源代码,你可能会看到这个。
简答
Fragment.getContext()
return activity 使用片段的上下文
详情
自 api 23 in Fragment
class 被引入 mHost
字段
// Activity this fragment is attached to.
FragmentHostCallback mHost;
并且 Fragment.getContext()
使用它来获取上下文:
/**
* Return the {@link Context} this fragment is currently associated with.
*/
public Context getContext() {
return mHost == null ? null : mHost.getContext();
}
在片段的 getContext()
方法中获得 Activity 的上下文之前有几个步骤。
1) 在 Activity 的初始化过程中 FragmentController
被创建:
final FragmentController mFragments = FragmentController.createController(new HostCallbacks());
2) 它使用 HostCallbacks
class(Activity
的内部 class)
class HostCallbacks extends FragmentHostCallback<Activity> {
public HostCallbacks() {
super(Activity.this /*activity*/);
}
...
}
3) 如您所见,mFragments
保留对 activity 上下文的引用。
4) 当应用程序创建片段时,它使用 FragmentManager
。它的实例取自 mFragments
(自 API 级别 23)
/**
* Return the FragmentManager for interacting with fragments associated
* with this activity.
*/
public FragmentManager getFragmentManager() {
return mFragments.getFragmentManager();
}
5) 最后在FragmentManager.moveToState(Fragment f, int newState, int transit, int transitionStyle, boolean keepActive)
方法中设置Fragment.mHost
字段
Fragment.getContext()
的文档说了
returns the context the Fragment is currently associated with.
它是在 api 23 年引入的 http://developer.android.com/reference/android/app/Fragment.html#getContext()
这是Application
还是Activity
Context
?
至于 FragmentActivity 和继承 - 'getContext()' 仍然会 return activity 上下文,如果你检查源代码,你可能会看到这个。
简答
Fragment.getContext()
return activity 使用片段的上下文
详情
自 api 23 in Fragment
class 被引入 mHost
字段
// Activity this fragment is attached to.
FragmentHostCallback mHost;
并且 Fragment.getContext()
使用它来获取上下文:
/**
* Return the {@link Context} this fragment is currently associated with.
*/
public Context getContext() {
return mHost == null ? null : mHost.getContext();
}
在片段的 getContext()
方法中获得 Activity 的上下文之前有几个步骤。
1) 在 Activity 的初始化过程中 FragmentController
被创建:
final FragmentController mFragments = FragmentController.createController(new HostCallbacks());
2) 它使用 HostCallbacks
class(Activity
的内部 class)
class HostCallbacks extends FragmentHostCallback<Activity> {
public HostCallbacks() {
super(Activity.this /*activity*/);
}
...
}
3) 如您所见,mFragments
保留对 activity 上下文的引用。
4) 当应用程序创建片段时,它使用 FragmentManager
。它的实例取自 mFragments
(自 API 级别 23)
/**
* Return the FragmentManager for interacting with fragments associated
* with this activity.
*/
public FragmentManager getFragmentManager() {
return mFragments.getFragmentManager();
}
5) 最后在FragmentManager.moveToState(Fragment f, int newState, int transit, int transitionStyle, boolean keepActive)
方法中设置Fragment.mHost
字段