3

In an Android app when I need to start a new activity, what's the difference on using the current activity context X application context?

magnomp
  • 353
  • 2
  • 8
  • Check out [this](http://stackoverflow.com/questions/1026973/android-whats-the-difference-between-the-various-methods-to-get-a-context) existing question – Patrick Kafka Aug 10 '11 at 16:53

3 Answers3

0

Activity extends Context, so you can use this. It doesn't matter for the usage of the activity, but if you initialize objects that will remain after the activity is used, you should use the application context.

MByD
  • 135,866
  • 28
  • 264
  • 277
0

The context of an application is the state your phone is in when you are within an application.

You use this context to refer to the application's UI elements and resources.

When you switch context, the application follows the Android application lifecycle.

You use the current context to start a new activity within an application.

Like this:

Intent i = new Intent(activity, com.MainApp.ActivityToStart);
currentActivity.startActivity(i);

Hope this helps.

Codeman
  • 12,157
  • 10
  • 53
  • 91
0

Application context has information about whole application life cycle while activity context has information about a particular activity. You should use activity context instead of Application context because it is the recommended way. Read this for more details

Android documentation says

public Context getApplicationContext ()
Return the context of the single, global Application object of the current process. This generally should only be used if you need a Context whose lifecycle is separate from the current context, that is tied to the lifetime of the process rather than the current component.

Haris Hasan
  • 29,856
  • 10
  • 92
  • 122