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()
???
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With