Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The Intent constructors parameters

The Intent class had 6 constructors

Intent()

Create an empty intent.


Intent(Intent o)

Copy constructor.


Intent(String action)

Create an intent with a given action.


Intent(String action, Uri uri)

Create an intent with a given action and for a given data url.


Intent(Context packageContext, Class cls)

Create an intent for a specific component.


Intent(String action, Uri uri, Context packageContext, Class cls)

Create an intent for a specific component with a specified action and data.

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)

When i want to start an Activity from a Fragment i do this:

Intent i = new Intent(getActivity(), DestinationActivity.class);

as far as i know, getActivity() will return an Activity

But the constructor expect a Context, how is this possible???

is it possible because of that the Activity that had returned by getActivity() implicitly invoke getApplicationContext()???

like image 482
hamid_c Avatar asked Aug 12 '15 16:08

hamid_c


People also ask

What are the parameters of Intent?

Intent parameters are used to identify and extract values within training phrases. These are specific words or phrases you want to collect from the user. Parameter name: The name associated with the parameter. This is used to reference the parameter in slot filling for scenes.

What are the two parameters that an Intent constructor takes when calling another activity?

The Intent constructor takes two parameters, a Context and a Class . The Context parameter is used first because the Activity class is a subclass of Context . The Class parameter of the app component, to which the system delivers the Intent, is, in this case, the activity to start.

What is Intent explain with example?

An intent is to perform an action on the screen. It is mostly used to start activity, send broadcast receiver,start services and send message between two activities. There are two intents available in android as Implicit Intents and Explicit Intents. Here is a sample example to start new activity with old activity.


1 Answers

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.

like image 93
androholic Avatar answered Sep 19 '22 18:09

androholic