What exactly does using the Application Context mean?

前端 未结 2 1232
自闭症患者
自闭症患者 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

提交回复
热议问题