1

I'm working on a code as a reference and encountered this Intent call: Intent(getApplicationContext(), secondActivity.class)

I got baffled because in my projects I only use: Intent(".mYSecondActivity")
I tried removing getApplicationContext() and changed it to Intent(".secondActivity")' in which the name is registered in the manifest. I run the application and Forced Closed. What is the significance of getApplicationContext()?

secondActivity.class is coded to retrieve data from a PHP MYSQL database.

droidH
  • 39
  • 1
  • 2
  • 10
  • When you move from one activity to another activity then you have to pass context from 1st activity to 2nd activity because both has different view and 2nd activity must have its own context. – The Holy Coder Feb 21 '13 at 07:12
  • [getApplicationContext()](http://developer.android.com/reference/android/content/Context.html#getApplicationContext()) – Paresh Mayani Feb 21 '13 at 07:14
  • Check provided link in my answer, you'll understand significance of every method of getContext. – The Holy Coder Feb 21 '13 at 07:17
  • I'm having a hard time understanding what context mean. Is it some form of data? @ManishAndroid – droidH Feb 21 '13 at 07:21
  • Context is a kind of base in form of object. It can be a base of application or a base of activity. Everything in android runs according to base. Like if you want to show a dialog so it needs context a base on which that dialog will be shown. so If you are getting application context you get the base of whole application but if you are getting activity context you will get activity base – Abhinav Singh Maurya Feb 21 '13 at 07:25
  • 1
    The answers here are all over the place. If you are starting activities by using `Intent(".mYSecondActivity")` then you must be defining intent-filters for all your activities. This is most probably **not** what you want to be doing. Please post your manifest so we can check this. – David Wasser Feb 21 '13 at 17:34
  • Actually, the significance of `Context` is probably irrelevant to this question. The real question should be: What is the difference between `new Intent("string")` and `new Intent(context, activity.class)` ? and actually, there is a big difference! – David Wasser Feb 21 '13 at 17:39
  • @ David Wasser - here is the manifest ` ` – droidH Feb 22 '13 at 06:50
  • @ David Wasser - I have been searching all night and I can't find a plain explanation in their difference. All I found are code snippets. No explanations. – droidH Feb 22 '13 at 06:52

2 Answers2

2

getApplicationContext() : Returns the context for the entire application (the process all the Activities are running inside of). Use this instead of the current Activity context if you need a context tied to the lifecycle of the entire application, not just the current Activity.

There is an excellent writeup in the first answer :

What's the difference between the various methods to get a Context?

What’s Context exactly? Per the Android reference documentation, it’s an entity that represents various environment data. It provides access to local files, databases, class loaders associated to the environment, services including system-level services, and more. Throughout this book, and in your day-to- day coding with Android, you’ll see the Context passed around frequently. From: "Android in Practice" book.

enter image description here

Community
  • 1
  • 1
The Holy Coder
  • 6,374
  • 8
  • 36
  • 71
0

"This provides a convenient way to create an intent that is intended to execute a hard-coded class name, rather than relying on the system to find an appropriate class for you." source: http://developer.android.com/reference/android/content/Intent.html#Intent%28android.content.Context,%20java.lang.Class%3C?%3E%29

Michał Z.
  • 4,099
  • 1
  • 21
  • 32