How can I start MAIN activity using <aim-filter>?

When I announce my main activity in this application:

<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 use 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 not used Intent i = new Intent(this, MyActivity.class);, how to do it with help actionfor<intent-filter>

Did not 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);
+3
source share
3 answers

Try to specify two target 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 your activity using the action name:

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

or class name:

Intent intent = new Intent(context, MyActivity.class);
context.startActivity(intent);
+5
source

The attribute namein the tag actionis the name of the action, not the name of your activity. delete the line

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

, , .

+1

com.package.name.MyActivity, com.package.name.general.MyActivity.

Intent . , .

You say you don’t want to create an intent by defining a class. I wonder why not - I think this is a good way to go.

0
source

Source: https://habr.com/ru/post/1525945/


All Articles