Below are the differences between Application context and Activity context
Application context and Activity context are the instances of Context but both of them are tied to different life cycles. Instance of Application context is tied to Application life cycle and gives the context of the entire application where an instance of Activity is tied to Activity life cycle.
The context of the Activity will not be available after the Activity gets destroyed however the Application context remains available.
When to use
Example:- Suppose ProcessDialog is to be shown in an activity, so in this case the activity context will be used as ProcessDialog is attached to that particular activity.
Activities runs inside of Application, so use Application context only when you want the context of the application and not just the activity.
How to get the Application context and Activity context
Application context: getApplicationContext() method is used to get the context of the Application.
Activity context: "this" keyword is used to get the context of the Activity.
0 Comment(s)