Understanding Android Context

All our main screens now have their layouts defined. We will now explain Android Context since each screen we just created represents one Context instance. If you go through the class definition and follow class extension, you will realize that each activity we create extends the Context class.

Context represents the current state of the application or object. It is used to access specific classes and resources of the application. For example, consider the following lines of code:

    resources.getDimension(R.dimen.header_height) 
    getString(R.string.app_name) 

Access we showed is provided by the Context class, which shows our activities are extending. Context is needed when we have to launch another activity, start a service, or send broadcast messages. We will show use of these methods when the time is proper. We already mentioned that each screen (Activity) of an Android application represents a Context instance. Activities are not the only classes that represent context. Except activities, we have the service context type too.

Android Context has the following purposes:

Context is an important part of Android and one of the most frequently used classes of the framework. Later in this book, you will meet other Context classes. However, before that, we will be focused on fragments and their explanation.