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

前端 未结 2 1096
清酒与你
清酒与你 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: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

提交回复
热议问题