For the More Curious: The Application Context

Earlier in this chapter, you used the Application Context in the constructor of the CrimeLab.

p​r​i​v​a​t​e​ ​C​r​i​m​e​L​a​b​(​C​o​n​t​e​x​t​ ​c​o​n​t​e​x​t​)​ ​{​
 ​ ​m​C​o​n​t​e​x​t​ ​=​ ​c​o​n​t​e​x​t​.​g​e​t​A​p​p​l​i​c​a​t​i​o​n​C​o​n​t​e​x​t​(​)​;​
 ​ ​.​.​.​
}​

What makes the Application Context special? When should you use the application context over an activity as a context?

It’s important to think about the lifetime of each of these objects. If any of your activities exist, Android will have also created an Application object. Activities come and go as the user navigates through your application but the application object will still exist. It has a much longer lifetime than ...

Get Android Programming: The Big Nerd Ranch Guide, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.