Which one to use Activity Context Or Application Context?

Many android developers get confused between activity and application context. Some types we are getting confused about which context we have to use and make the android resources a more efficient way.

As we know two types of context application and activity context. Both the context are extending the super-class context.


Activity Context

It represents the current activity information and also depends upon the activity life cycle. If you want to perform some operation based on activity life span then we are using it. Mostly in the application when we need to start a new activity or show dialog than we can use this kind of context. 

Application Context 

The life span of this context is based on the application state. It does not depend upon the activity life span, So if the activity is killed or destroyed then it does not affect the application context. The context has become available when you are launching the application and have the value until the application not gets destroyed.   

Conclusion

If you have functionality that does not depend on the activity life-cycle then you can use the application context. Used to show toast messages or start services. If you have functionality that will be destroyed as soon as the activity is finish or destroyed then you have to use activity context.

Comments