What is the difference betweeen Intent(getApplicationContext(), class) and Intent(class)

前端 未结 2 1095
清酒与你
清酒与你 2021-01-25 19:21

I\'m working on a code as a reference and encountered this Intent call: Intent(getApplicationContext(), secondActivity.class)

I got baffled because in my pr

相关标签:
2条回答
  • 2021-01-25 20:00

    "This provides a convenient way to create an intent that is intended to execute a hard-coded class name, rather than relying on the system to find an appropriate class for you." source: http://developer.android.com/reference/android/content/Intent.html#Intent%28android.content.Context,%20java.lang.Class%3C?%3E%29

    0 讨论(0)
  • 2021-01-25 20:01

    getApplicationContext() : Returns the context for the entire application (the process all the Activities are running inside of). Use this instead of the current Activity context if you need a context tied to the lifecycle of the entire application, not just the current Activity.

    There is an excellent writeup in the first answer :

    What's the difference between the various methods to get a Context?

    What’s Context exactly? Per the Android reference documentation, it’s an entity that represents various environment data. It provides access to local files, databases, class loaders associated to the environment, services including system-level services, and more. Throughout this book, and in your day-to- day coding with Android, you’ll see the Context passed around frequently. From: "Android in Practice" book.

    enter image description here

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