Reputation: 1551
Intent intent = new Intent(activityContext,SomeActivity.class);
startActivity(intent);
instead of activityContext can we use applicationContext, if yes, what will be the difference in newly created activity.
I know the difference between activity context and application context, but I want to know how it will effect activity if we start it using an application context.
Upvotes: 4
Views: 2908
Reputation: 9225
Application Context and Activity context are instance of Context class. Application Context refers to Application life cycle and Activity context refers to the Activity life cycle. So, for getting current activity information you may need to use Activity context instead of the Application Context.
Upvotes: -1
Reputation: 371
No difference in newly created Activity as They are both instances of Context, but can create non-standard back stack behaviors in your application and also the application instance is tied to the lifecycle of the application, while the Activity instance is tied to the lifecycle of an Activity. But in general, use the activity context unless you have a good reason not to.
Upvotes: -1
Reputation: 213
Now the thing that confuses is the declaration of different contexts and their specific-usage. To make things simple, you should count two types of context available in the Android framework.
Application Context Activity Context Application context is attached to the application's life-cycle and will always be same throughout the life of application. So if you are using Toast, you can use application context or even activity context (both) because a toast can be raised from anywhere with in your application and is not attached to a window.
Activity context is attached to the Activity's life-cycle and can be destroyed if the activity's onDestroy() is raised. If you want to launch a new activity, you must need to use activity's context in its Intent so that the new launching activity is connected to the current activity (in terms of activity stack). However, you may use application's context too to launch a new activity but then you need to set flag Intent.FLAG_ACTIVITY_NEW_TASK in intent to treat it as a new task.
Now referring to your cases:
activitycontext: though its referring to your own class which extends Activity class but the base class (Activity) also extends Context class, so it can be used to offer activity context.
getApplication() though its referring to Application object but the Application class extends Context class, so it can be used to offer application context.
getApplicationContext() offers application context.
getBaseContext() offers activity context.
Go for activity context when using Intent and for toast you can use any context.
Upvotes: 2