Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly does using the Application Context mean?

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!

like image 942
Chris P Avatar asked Mar 08 '11 03:03

Chris P


People also ask

What do you mean by application context?

The ApplicationContext is the central interface within a Spring application that is used for providing configuration information to the application. It's created when the application starts running.

What is the use of application context xml?

Applicationcontext. xml - It is standard spring context file which contains all beans and the configuration that are common among all the servlets. It is optional file in case of web app. Spring uses ContextLoaderListener to load this file in case of web application.

What is the difference between application context and activity context?

They are both instances of Context, but the application instance is tied to the lifecycle of the application, while the Activity instance is tied to the lifecycle of an Activity. Thus, they have access to different information about the application environment.


1 Answers

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

like image 103
hackbod Avatar answered Oct 06 '22 00:10

hackbod