Passing Activity or Context to other instance

后端 未结 5 1797
渐次进展
渐次进展 2020-12-19 05:36

Can you please tell me whats the best practice to pass activity or context to other instance(;)

But...,

  • What is better to pas
5条回答
  •  [愿得一人]
    2020-12-19 05:53

    Context is an entity which provides global information about application or activity. You can get the context by invoking getApplicationContext(), getContext(), getBaseContext() or this (when in the activity class). Context is tied with lifecycle of its activity/application and commonly used for creating new objects, accessing resources e.t.c.

    In your code you can use Context as a first parameter in Toast.makeText . It's also common to have global Context object for a class. You may read some explanation about Context usage here

    You may need Activity object for example if you need to start new activity and create Intent object.

提交回复
热议问题