Difference between Android Activity Context and Application Context


What is Context?

A Context gives us information about the current state of an application. It allows us to interact with Android Components. It allows us to access files and other resources such as pictures, Activities, Fragments and Services. The Context Class provides access to several resources and services that are needed to build an Android application including −

  • Resources − The resources such as layouts, images and strings which are stored in resource objects are accessed through the Context.

  • System Services − It provides access to system services such as Power Manger, Notification Manager and Location Manager.

  • Preference − The Preference is used to store and retrieve data in key value pair which can be accessed through Context.

  • Asset Manager − It is used to access raw asset files that are packaged with the application.

  • Databases − it provides access to the database used by the application.

What are the two types of Context?

There are two types of context which are as follows −

  • Activity Context

  • Application Context

In Android, both “Activity” and “Application” are subclasses of the “Context”class, which provides access to application-specific resources and classes.

The primary difference between both of this context is that “Activity” context is tied to the lifecycle of an “Activity”, while “Application” context is tied to the lifecycle of the entire application

What is Activity Context in Android?

When you use any “Activity” context, it is tied to the activity that you are currently in. This means that if the activity is destroyed, the context will also be destroyed. An activity context should be used when you need access to resources or classes that are tied to a specific activity, such as views or resources that are specific to that activity.

Functionalities Provided by Activity Context

  • Launching Intent − It is used to start a new activity or services using startActivity() or startActivityForResult() method.

  • Resource Access − It provides access to resources such as strings, layouts and images through getResources() method.

  • View Inflation − Activity Context is used to inflate views with the help of LayoutInflator.

  • Dialog Creation − It is used to create Alert Dialog Box and Progress Dialog Box.

  • Action Bar − It is used to manage Action Bar such as setting title, icons and navigation.

What is Application Context in Android?

On the other hand, “Application” context is tied to the application itself and remains alive as long as the application is running. This means that it can be used across multiple activities, and is useful for accessing global resources and classes that are not tied to any specific activity, such as shared preferences or database helpers.

Functionalities Provided by Application Context

  • Resource Access − It provides access to application resources such as layouts, dimensions, strings through Resource class.

  • Application level - information access − It provides information related to the application such as package name, application name, application version through Package Manager class.

  • Application-level-Broadcasts − It is used to send and receive application-level broadcasts using registerReceiver() and sendBroadCast() methods.

  • Resource Caching − It uses resources such as layouts and drawables to improve the application performance.

Difference between Activity Context and Application Context

Activity Context

Application Context

Activity Context has a shorter life cycle than Application Context. It is created and destroyed with the Activity.

Application Context is created when the application starts and remains till the application gets terminated.

Activity Context has a shorter life cycle and can be destroyed with the activity.

Application Context has a longer life cycle and remains in the memory throughout the lifetime of an application.

Activity Context is limited to current Activity such as Fragments and Views.

Application Context is available to the entire application including all activities and their components.

Conclusion

In summary, if you need to access resources that are tied to a specific activity, use the Activity context. If you need to access resources that are not tied to any specific activity, use the Application context.

Updated on: 09-May-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements