6

When I declare my main activity in this maner:

<activity android:name=".MyActivity"
          android:configChanges="orientation|keyboardHidden"
          android:windowSoftInputMode="stateHidden"
          android:screenOrientation="portrait">
    <intent-filter>
          <action android:name="android.intent.action.MAIN"/>
          <action android:name="com.package.name.MyActivity"/>
          <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

then I get an error No Activity found to handle Intent { act=com.package.name.MyActivity flg=0x24000000 } when I am using this code:

Intent intent = new Intent("com.package.name.MyActivity");
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(intent);

If do not use Intent i = new Intent(this, MyActivity.class); how can I do this with the help of action for <intent-filter>

Didn't help:

 <intent-filter>
      <action android:name="android.intent.action.MAIN"/>
      <action android:name="com.package.name.VIEW"/>
      <category android:name="android.intent.category.LAUNCHER" />
 </intent-filter>

code:

Intent intent = new Intent("com.package.name.VIEW");
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(intent);
Sviatoslav
  • 1,301
  • 2
  • 17
  • 42
  • Create a custom 'action' such as `com.package.name.ACTION_VIEW` and as Jeffery Blatman advises, remove the 'action' for `com.package.name.MyActivity`. You then just use your custom action to start the `Activity`. – Squonk Jul 28 '12 at 17:28
  • I've changed the code, but it didn't help me. where I made ​​a mistake? – Sviatoslav Jul 28 '12 at 18:13

3 Answers3

8

Try to specify two intent filters:

<activity android:name=".MyActivity"
          android:configChanges="orientation|keyboardHidden"
          android:windowSoftInputMode="stateHidden"
          android:screenOrientation="portrait">
    <intent-filter>
          <action android:name="android.intent.action.MAIN"/>
          <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    <intent-filter>
          <action android:name="com.package.name.MyAction"/>
          <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
</activity>

Then you can start the activity using the action name:

Intent intent = new Intent("com.package.name.MyAction");
context.startActivity(intent);

or the class name:

Intent intent = new Intent(context, MyActivity.class);
context.startActivity(intent);
Vincent Mimoun-Prat
  • 28,208
  • 16
  • 81
  • 124
  • `Intent intent = new Intent("com.package.name.MyAction"); context.startActivity(intent);` didn't help (_No Activity found to handle Intent_) :( Using class name work, but I want to use **action name** – Sviatoslav Jul 28 '12 at 18:40
  • @Svyatoslav: I am very confident that the answer given here is correct, and that you implemented the solution incorrectly. – CommonsWare Jul 28 '12 at 18:55
  • 5
    @CommonsWare I found that it works by using **action name** when second `` contains else. – Sviatoslav Jul 28 '12 at 19:50
  • 4
    @Svyatoslav: Ah, yes, my apologies. `startActivity()` always adds `CATEGORY_DEFAULT` to an `Intent` if there is no other category specified. Hence, an `` for an `` always needs a ``, whether `DEFAULT` or something else. – CommonsWare Jul 28 '12 at 19:54
  • 1
    Answer edited to add the default category. Sorry for having forgotten that. – Vincent Mimoun-Prat Jul 28 '12 at 20:57
1

the name attribute in the action tag is the name of the action, not the name of your activity. remove the line,

  <action android:name="com.package.name.MyActivity"/>

since the intent filter tag is under your activity's tag, the system already understands that it's applied to that activity.

Jeffrey Blattman
  • 22,176
  • 9
  • 79
  • 134
0

Your class name isn't com.package.name.MyActivity, it's com.package.name.general.MyActivity.

An easy way to avoid this mistake is to create your new Intent using a class instead of a package name. You can use auto-complete to fill in the right class.

You say you don't want to create the Intent by specifying the class. I wonder why not -- I think it's a good way to go.

Sparky
  • 8,437
  • 1
  • 29
  • 41