Search code examples
androidandroid-intentandroid-implicit-intent

Calling Main Activity using Implicit Intents (specifying action and category)


I'm trying to call main activity using implicit intents. I give both action and category in intent but before starting the activity android system gives me a list of applications to select from for opening the activity.

Code snippet I am using to call the main activity follows:

protected void initiateActivity(int requestCode, String value, String oper) {
        Intent i = new Intent("android.intent.action.MAIN");
        i.addCategory("android.intent.category.LAUNCHER");
        i.putExtra("VALUE", value);
        i.putExtra("OPER", oper);
        startActivityForResult(i, requestCode);
    }

It seems to me that every app in system will be having same action, category combo, hence android is giving me that list of apps to select from. What changes can I make to my Main Activity so that this issue is not seen?


Solution

  • Looks like you might need to separate out with intent-filters. It looks there is a good explanation in this post:

    How can I start MAIN activity with the help of <intent-filter>?

    Which recommends adding the filters such as these below or you will be calling the launcher:

    <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>