In my attempts to find out how to start a new intent in my app, I\'ve come across several ways of phrasing it.
This syntax returns a runtime error, namely a Activity
Please note, when you use "this" key word in "Intent in = new Intent(this, SomeActivity.class); " make sure you are not in a sub class of MainActivity this can set the wrong Activity as part of the Explicit intent Component.strong text
Basic information about Intent resolution
Intents can contain the following basic information:
There are 2 ways that Intents are resolved by the system:
If you specify the component (package name and class name) then this is used to explicitly find the activity you have specified and the Intent is sent to that activity. The other Intent data is not used (although it is passed to the called activity in the Intent). This is called "explicit Intent resolution".
If you don't specify the component, then the ACTION, CATEGORY and DATA fields are used to locate one or more activities that advertise (via intent-filter) that they can accept the Intent. This is called "implicit Intent resolution".
To your specific questions
When you do this:
Intent in = new Intent("com.something.something");
You are creating an implicit Intent and setting the ACTION to "com.something.something". If you then call startActivity()
with this Intent, you get ActivityNotFoundException
because Android cannot find an activity that can accept an Intent with ACTION="com.something.something". The reason is because you have provided an intent-filter with ACTION="com.something.something" and CATEGORY="android.intent.category.LAUNCHER" but you haven't specified the CATEGORY in your Intent (Android automatically adds the CATEGORY "DEFAULT" to an Intent if there isn't any CATEGORY specified when using startActivity()
). To make this work you should either
CATEGORY="android.intent.category.LAUNCHER"
with CATEGORY="android.intent.category.DEFAULT"
or<category android:name="android.intent.category.DEFAULT" />
to the intent-filter for SecondActivity
When you do this:
Intent in = new Intent(MainActivity.this, SecondActivity.class);
You are creating an explicit Intent specifying the component SecondActivity
. The signature for this method is Intent(Context packageContext, Class clas)
. It uses the package name from packageContext
and the class name from clas
to create an explicit Intent for that component. If you use this constructor inside an Activity, you can just use this
as the first parameter, because Activity
extends Context
. If you use this constructor from another class (like an OnClickListener
) you need to specify MyActivity.this
as the first parameter to pass the instance of the Activity and not of the OnClickListener (because OnClickListener
does not extend Context
).
When you do this:
Intent in = new Intent().setClass(this, SecondActivity.class);
you are creating an explicit Intent as above. This is exactly the same as using:
Intent in = new Intent(this, SecondActivity.class);
You can't do this inside an OnClickListener
because the first parameter needs to be a Context
(or a class that extends Context
, like Activity
).
If you want to create an explicit Intent you can also use this:
Intent in = new Intent().setClassName("com.something", "com.something.SecondActivity");
This creates an explicit intent, but you don't need a Context
for this. You can just pass the package name and the class name as Strings (if you know them).
For more information about Intent resolution, see: