14

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()???

hamid_c
  • 849
  • 3
  • 11
  • 29
  • 3
    Look in the API Docs how Context is inerhited. You can travel the back to context from activity or application or fragment. Any of these are also contexts ;) – Rene M. Aug 12 '15 at 16:39

3 Answers3

8

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.

androholic
  • 3,633
  • 3
  • 22
  • 23
6

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.

Community
  • 1
  • 1
Danilo Prado
  • 2,260
  • 1
  • 13
  • 16
1

Activity extends Context so you can just cast it:

Intent i = new Intent((Context)getActivity(), DestinationActivity.class);
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • a@Marcin Orlowski. I hope you can fix my problem. I'm creating an intent specific to a component. I'm getting a wavy red line below the second paramater which is my service class name in the following code. Intent wifi_intent = new Intent(Application.Context,WifiMonitoringService.class); Its says "; expected " error when I place the mouse over that. Please help me out. I have posted that question. You can find it in the following link http://stackoverflow.com/questions/40828123/couldnt-pass-the-service-class-to-the-intent-constructor-intentcontext-class – babybob Nov 27 '16 at 10:59