The Intent constructors parameters

前端 未结 3 1871
醉话见心
醉话见心 2021-02-19 06:52

The Intent class had 6 constructors

Intent()

Create an empty intent.


Inten

相关标签:
3条回答
  • 2021-02-19 07:36

    Take a look at the argument Context very closely in the fifth Intent declaration. It reflects polymorphism. The Intent takes a Context argument so you can pass any object that is a Context or derives from the Context class.

    Activity, AppCompatActivity, IntentService, Service all derive from the Context class and hence can be passed as an argument to the method.

    0 讨论(0)
  • 2021-02-19 07:41

    Activity extends Context so you can just cast it:

    Intent i = new Intent((Context)getActivity(), DestinationActivity.class);
    
    0 讨论(0)
  • 2021-02-19 07:43

    Activity inherits context. Thus, if you are in an activity, you only need to pass itself to use the context. It also contains a pointer to getBaseContext(). You might occasionally need to reference that, if you need the entire application context, but most likely you won't for a while.

    You can find more details about the Activity class here.

    This question about the intent constructor parameters is similar to yours and has a really good answer. I think you'd like to check it out.

    Hope it helps.

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