Intent构造函数参数 [英] The Intent constructors parameters
问题描述
Intent类具有6个构造函数
The Intent class had 6 constructors
Intent()
创建一个空的意图.
意图(意图o)
复制构造函数.
意图(字符串操作)
使用给定的动作创建意图.
Create an intent with a given action.
意图(字符串操作,Uri uri)
使用给定的操作和给定的数据url创建意图.
Create an intent with a given action and for a given data url.
意图(上下文packageContext,类cls)
为特定组件创建意图.
意图(字符串操作,Uri uri,Context packageContext,Class cls)
使用指定的操作为特定组件创建意图,然后数据.
Create an intent for a specific component with a specified action and data.
我几乎是android编程的新手,当我需要启动另一个 Activity
或 Fragment
:
I'm almost new in android programming and mostly using the fifth one when i need to start another Activity
or Fragment
:
Intent(Context packageContext, Class<?> cls)
当我想从 Fragment
开始 Activity
时,我会这样做:
When i want to start an Activity
from a Fragment
i do this:
Intent i = new Intent(getActivity(), DestinationActivity.class);
据我所知, getActivity()
将返回 Activity
但是构造函数希望使用 Context
,这怎么可能??
But the constructor expect a Context
, how is this possible???
是否有可能是因为 getActivity()
返回的 Activity
隐式调用了 getApplicationContext()
???
is it possible because of that the Activity
that had returned by getActivity()
implicitly invoke getApplicationContext()
???
推荐答案
在第五个Intent声明中非常仔细地查看参数 Context
.它反映了多态性. Intent
使用一个 Context
参数,因此您可以传递任何为 Context
或从 Context
类派生的对象.
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都从 Context
类派生,因此可以作为参数传递给该方法.
Activity, AppCompatActivity, IntentService, Service all derive from the Context
class and hence can be passed as an argument to the method.
这篇关于Intent构造函数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!