user2882662
user2882662

Reputation: 535

What is App Context?

I don't precisely understand what the Context of app is, what it provides.

The description given in the Android API is:

"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."

  1. What is global information?
  2. What doe we mean by application environment?
  3. What do we mean by up-calling for app-level operations such as launching activities?

Upvotes: 2

Views: 2398

Answers (1)

Yevgeny Simkin
Yevgeny Simkin

Reputation: 28349

The Application is a class that lives alongside all your Activities. Think of it as the basement (or the attic). It is created before any other thing that your manifest contains (Activities, Services, etc) and is the last thing cleaned up if your app is GCed by Android.

So you can use it to store persistent variables and data members that various Activities need to have access to.

To use it, create a new class that extends Application (MyApp.java) and reference it in the application node of the manifest (android:name=".MyApp").
Then you can get a reference to it in onCreate of any Activity like

super.onCreate(b);
MyApp myApp = (MyApp)getApplicationContext();

Make sure not to confuse the Activity or Service Context with the Application Context. they're frequently interchangeable but if you're not careful you'll wind up raising exceptions related to your UI Thread.

Upvotes: 1

Related Questions