11

I'm new to this and I'm sorry if this is a really dumb question. I'm just trying to clarify things. My book says I can retrieve application context for process by using the getApplicationContext() method. I just really don't know where to type this or what to do with any of it. I can go to the hierarchy but what do I do with all the script there. Also where would I write Activity Callbacks, in the main.xml? An exercise wants me to add a logging tag to my project but I'm not sure how to do this. The exact text says:

"Within the onCreate() callback method, add an informational logging message, using the Log.i() method."

and another exercise says to:

"Implement some of the Activity callback methods in addition to onCreate(), such as onStart(). Add a log message to each callback method and then run the application normally".

As these seem like basic questions, can someone please help me.

I am using the Android SDK, and Eclipse. I have made the Hello World application, but I have no idea what to do with Context or Retrieving resources. Please help!

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Chris P
  • 111
  • 1
  • 1
  • 3
  • [`ApplicationContext` javadocs](http://www.androidjavadoc.com/m3-rc37a/android/app/ApplicationContext.html) – Matt Ball Mar 08 '11 at 04:21
  • 2
    Good lord, you just pointed to documentation for a PRE-1.0 version of the platform, for a class that was not even part of the SDK. – hackbod Mar 08 '11 at 04:50
  • I recognize those exercises. Is your book, by any chance, "Sam's Teach Yourself... in 24 Hours"? I have that one, and when I finished it, I felt like there were some very crucial, fundamental topics that really hadn't been covered. You might want to take a look [at some alternatives](http://stackoverflow.com/questions/3119665/is-the-book-sams-teach-yourself-android-application-development-in-24-hours-wor). – erichamion Mar 08 '11 at 07:58
  • @erichamion--You are correct, sir. Sam's ... Java in 24 Hours is terrible. Some alternatives--almost ANY alternative would be better, but the link for alternatives is broken. In authors' collective defense, Java's huge and they have to pick and choose what to include. But Sam's narrowed it down WAY too far. Even so, 24 hours of work time ... hmmmm... 3.5 hours per day for a week; 8 hours a day for 3 days. And the examples leave so much to the LEARNING user that they often won't compile because of omissions. – DSlomer64 Aug 18 '18 at 16:15

2 Answers2

26

The first rule I would give you: if you don't know why you need it, you probably don't need it. Use your activity object as the Context when you need a context.

The callbacks you talk about are on the Activity class. The Application Fundamentals describes what an Activity is: http://developer.android.com/guide/topics/fundamentals.html#Components

The only time you want to use getApplicationContext() is when you need a Context that exists outside of the lifecycle of an Activity class (or other component). You'll want to find documentation on specific cases where this is desired, there is a lot floating around. For example this one is part of the Android documentation: http://android-developers.blogspot.de/2009/01/avoiding-memory-leaks.html

Lennart
  • 9,657
  • 16
  • 68
  • 84
hackbod
  • 90,665
  • 16
  • 140
  • 154
  • 1
    The other part of the question was about the logging tag. The tag is simply a string you can define, perhaps as a `static final String` in your Activity class. Then you would call, for example, `Log.i(LOG_TAG, "onStart() was called. Hooray!")` – erichamion Mar 08 '11 at 08:03
  • 11
    So, your answer is to use the activity context. But you link to an articale that recommends in general to use the application context instead, to avoid memory leaks? I am a _bit_ confused right now. What is the advantage of using the activity context? – JacksOnF1re Mar 18 '15 at 12:56
0

For the tasks you're working with here, you'll be using the Java code that defines the behavior of the application, not the XML files that define resources and layouts or the AndroidManifest.xml file that declares basic application properties.

If you're working with Hour 3 of the Sam's Teach Yourself... book, then you need to open the src\com.androidbook.droid1\DroidActivity.java file. In general, you would need src\<package-name>\<class-name>.java. When you open that file, you'll see a class (in this case, DroidActivity) that extends Activity and already has the onCreate() callback method. Anything that you want to happen during onCreate() goes inside that method. Other callback methods can be added inside the activity class. To see an example that has all the lifecycle callbacks (but doesn't do anything in them), look here.

A logging tag is just a string. You can declare it, for example, as a private static final String inside the activity class.

If there's confusion about where methods belong, where and how to define variables or constants, how to call methods, how to use classes, and so forth, then it might be best to go through an introductory Java text before starting with Android. There are plenty of free resources available for that.

Community
  • 1
  • 1
erichamion
  • 4,517
  • 1
  • 21
  • 16