What exactly does using the Application Context mean?

前端 未结 2 1217
自闭症患者
自闭症患者 2020-12-05 20:47

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 t

相关标签:
2条回答
  • 2020-12-05 21:04

    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

    0 讨论(0)
  • 2020-12-05 21:17

    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.

    0 讨论(0)
提交回复
热议问题