问题描述
我很困惑背景和活动之间的不同。什么情况下呢?确实方面属于一个活动?将上下文的变化,如果该活动是改变了吗?
I am confused to differ between context and activity. what is context anyway? does context belongs to one activity ? will the context change if the activity is changed?
我有一个游戏,有多个活动。每个活动的每个部分,启动画面,菜单,gamescreen等。现在的问题是我有一个单身SoundManager类它使用的Soundpool,因为它的声音播放器。我想加载在启动画面每一个声音。但在我认为它再次,又有怎样的背景?
I have a game which has multiple activity. each activity for each part, splashscreen, menu, gamescreen , etc.The problem is I have one singleton SoundManager which use soundpool as it's sound player. I want to load every sound in splashscreen. But after I think it once again, how about the context?
public void loadSound(Context context, int resId, String name) {
int id = sounds.load(context, resId, priority)
}
如果我加载的声音在闪屏的活动,我怎么会打在GameScreen活动的声音?不同的上下文是不是?
if I load the sounds in SplashScreen Activity, how could I play the sound in GameScreen Activity? different context isn't it?
推荐答案
每:http://android-developers.blogspot.com/2009/01/avoiding-memory-leaks.html
在Android上,上下文是用于许多 操作,但主要是加载和 接入资源。这就是为什么所有的 小部件接收的上下文参数 它们的构造函数。在常规 Android应用程序,你通常有 2种背景下,活动和 应用。
和从 Android的文档:
它允许访问 应用特定的资源和 类,以及向上呼吁 应用层的操作,如 发射活动,广播和 接收意向等。
通常,每个活动都会有它自己的上下文和应用程序本身将有一个语境。
Typically each Activity will have it's own Context and the Application itself will have a Context.
这篇关于是一个活动=一个上下文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!