3

Possible Duplicate:
Difference between Activity Context and Application Context

When to use Context and Activity. I have read code that uses both Context and Activity as parameters in Constructor like below,please clear me on this

public AmazedView(Context context, Activity activity) {
        super(context);

        mActivity = activity;

        // init paint and make is look "nice" with anti-aliasing.
        mPaint = new Paint();
        mPaint.setTextSize(14);
        mPaint.setTypeface(mFont);
        mPaint.setAntiAlias(true);

        // setup accelerometer sensor manager.
        mSensorManager = (SensorManager)                activity.getSystemService(Context.SENSOR_SERVICE);
        // register our accelerometer so we can receive values.
        // SENSOR_DELAY_GAME is the recommended rate for games
        mSensorManager.registerListener(mSensorAccelerometer, SensorManager.SENSOR_ACCELEROMETER,
                SensorManager.SENSOR_DELAY_GAME);

        // setup our maze and marble.
        mMaze = new Maze(mActivity);
        mMarble = new Marble(this);

        // load array from /res/values/strings.xml
        mStrings = getResources().getStringArray(R.array.gameStrings);

        // set the starting state of the game.
        switchGameState(GAME_INIT);
    }
Community
  • 1
  • 1
amj
  • 367
  • 1
  • 8
  • 26
  • Yes @A--C but my question here basically is why do we need both activity and context in the constructor?How activity can be made use of here? – amj Dec 14 '12 at 02:39

3 Answers3

13

Context: is a handle to the system. Context contains environment data such as local files, database, ... Context also include many system services. For example, Context will provide access to Location Service,... Also, with context, you can use resources, access to databases and preferences, local data ...

Activity: extends from Context. And not only Activity, many others extends Context, and each has its own purpose such as: FragmentActivity, Service, WallpaperService. More detail in Activity, "normal" android app has activity. It's like a handle to the environment your application is currently running in. Activity can create UI (User Interface)

In your above code, depends on other constructor, you should put context or activity object into. And as you see, if activity takes from the same context, you can use one parameter in the constructor

public AmazedView(Context context) {
   Activity activity = (Activity) context;
}

But you can see, it's not clear, and "hide" real object behind. Because Activity is the subclass of context, so in an informal way, Activity has some "additional things" that context doesn't have. If you put it as a context object, no one knows that fact and will make the code seems obscure. Moreover, context might become from Service for example, and you will don't sure when typecasting to Activity. So, make two parameters is suitable here.

More importantly, you should carefully to use Activity as the context object because it might cause a memory leak.

hqt
  • 29,632
  • 51
  • 171
  • 250
5

Application context refers to the application environment and the process within which all its components are running. It allows applications to share the data and resources between various building blocks.An application context gets created whenever the first component of this application is started up regardless of whether that component is an activity, service, or something else.Application context lives as long as your application is alive. As such, it is independent of the activities life cycle. You can easily obtain a reference to the context by calling

  Context.getApplicationContext() or Activity.getApplication()

keep in mind that activities and services are already subclasses of context, and as such they inherit all its methods.

An activity is usually a single screen that the user sees on the device at one time. An application typically has multiple activities, and the user flips back and forth among them. As such, activities are the most visible part of your application.

you can also take a look at this : What is 'Context' on Android?

Community
  • 1
  • 1
Mahdi Giveie
  • 630
  • 1
  • 8
  • 25
1

On the developer page of android: http://developer.android.com/reference/android/content/Context.html it states:

"[A context is an] Interface to global information about an application environment. This is an abstract class whose implementation is provided by the Android system. 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. "

An Activity is an indirect subclass of Context, as mentioned on the page.

Jeeter
  • 5,887
  • 6
  • 44
  • 67