是一个活动=一个上下文? [英] Is one Activity = one Context?

查看:187
本文介绍了是一个活动=一个上下文?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很困惑背景和活动之间的不同。什么情况下呢?确实方面属于一个活动?将上下文的变化,如果该活动是改变了吗?

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?

推荐答案

每:<一href="http://android-developers.blogspot.com/2009/01/avoiding-memory-leaks.html">http://android-developers.blogspot.com/2009/01/avoiding-memory-leaks.html

在Android上,上下文是用于许多   操作,但主要是加载和   接入资源。这就是为什么所有的   小部件接收的上下文参数   它们的构造函数。在常规   Android应用程序,你通常有   2种背景下,活动和   应用。

On Android, a Context is used for many operations but mostly to load and access resources. This is why all the widgets receive a Context parameter in their constructor. In a regular Android application, you usually have two kinds of Context, Activity and Application.

和从 Android的文档

它允许访问   应用特定的资源和   类,以及向上呼吁   应用层的操作,如   发射活动,广播和   接收意向等。

It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.

通常,每个活动都会有它自己的上下文和应用程序本身将有一个语境。

Typically each Activity will have it's own Context and the Application itself will have a Context.

这篇关于是一个活动=一个上下文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆