startActivity(this, OtherActivity.class) 如何使用 Context 对象启动一个活动?
Posted
技术标签:
【中文标题】startActivity(this, OtherActivity.class) 如何使用 Context 对象启动一个活动?【英文标题】:how does startActivity(this, OtherActivity.class) use Context object to start an activity? 【发布时间】:2016-01-12 02:11:54 【问题描述】:我开始阅读 Context 我有点困惑。
我找到了以下定义。
The Context class is an “Interface to global information about an application environment.”
这里我不明白什么是android应用环境?
我们可以使用1。 getAssets() 2. getResources() 3. getPackageManager() 4. getString() 5. getSharedPrefsFile() 方法,我们也可以启动一个activity,使用Context广播一个Intent。
我认为,以上方法(粗体)将应用的全局数据提供给应用,
这些资源是否称为 App 全局数据?
那么对于启动 Activity,Android 使用了哪些全局信息 系统?
视图如何使用上下文(它从上下文中获取什么信息)?
我对 Context 类的概念以及所有这些东西之间的关系感到非常困惑。
请提供任何帮助。
【问题讨论】:
【参考方案1】:请查看上下文详细信息。我认为这些东西可以帮助你理解上下文的概念。
View.getContext():
Returns the context the view is currently running in. Usually the currently active Activity.
Activity.getApplicationContext():
Returns the context for the entire application (the process all the Activities are running inside of). Use this instead of the current Activity context if you need a context tied to the lifecycle of the entire application, not just the current Activity.
ContextWrapper.getBaseContext():
If you need access to a Context from within another context, you use a ContextWrapper. The Context referred to from inside that ContextWrapper is accessed via getBaseContext().
【讨论】:
以上是关于startActivity(this, OtherActivity.class) 如何使用 Context 对象启动一个活动?的主要内容,如果未能解决你的问题,请参考以下文章
从 Activity 外部调用 startActivity()?
在 startActivity() 上传递一个 Bundle?