Kaloyan Roussev
Kaloyan Roussev

Reputation: 14711

How to add extras to manifest-declared intent

I have added Nougat shortcuts to my Android app but all of the shortcuts want to start the same activity, but with a different parameter (then, the activity decides which fragment to add)

So, in the res\xml-v25\shortcuts.xml file, I have 4 shortcut elements described, they all have the same intent

<intent
    android:action="android.intent.action.VIEW"
    android:targetClass="com.example.MyActivity"
    android:targetPackage="com.example"
/>

So how do I pass a bundle with extras to that activity? I tried with <meta-data>, <data> and one more thing, I forgot what it was, but the intent in onCreate and onNewIntent always comes empty.

<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
    <shortcut
        android:enabled="true"
        android:icon="@drawable/feature_1"
        android:shortcutId="feature_one"
        android:shortcuDsaibledMessage="Disabled"
        android:shortcutShrotLabel="Feature 1"
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="com.example.MyActivity"
            android:targetPackage="com.example"
         <!-- <extra -->
              <!-- android:name="action" -->
              <!-- android:value="feature_1"/> -->
        </intent>
    <shortcut>
    // then 3 more shortcuts like this with feature 2, 3 and 4

Upvotes: 4

Views: 2897

Answers (2)

Kaloyan Roussev
Kaloyan Roussev

Reputation: 14711

Don't know why, but @venimania 's answer didn't help me, maybe I was doing something wrong. I was able to resolve my problem by using a custom defined action of my own:

<intent
        android:action="com.example.FEATURE_ONE"

and then in onCreate

String action = getIntent().getAction();

Upvotes: 2

venimania
venimania

Reputation: 108

You should add one extra for each shorcut so when you receive the intent in your activity you can filter by that: for example:

<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
  <shortcut
    android:shortcutId="action1"
    android:enabled="true"
    android:icon="@drawable/compose_icon"
    android:shortcutShortLabel="@string/compose_shortcut_short_label1"
    android:shortcutLongLabel="@string/compose_shortcut_long_label1"
    android:shortcutDisabledMessage="@string/compose_disabled_message1">
    <intent 
    android:action="android.intent.action.VIEW"
    android:targetClass="com.example.MyActivity"
    android:targetPackage="com.example"/>
    <!-- If your shortcut is associated with multiple intents, include them
         here. The last intent in the list determines what the user sees when
         they launch this shortcut. -->
    <categories android:name="android.shortcut.conversation" />
    <extra
            android:name="accion"
            android:value="action_1_fragment1" />
  </shortcut>
  <!-- Specify more shortcuts here. -->
</shortcuts>

And then within your activity:

String action = getIntent().getStringExtra("accion");
if(action.equals(ACTION_1))
     //Do action 1
else
     //do action 2 

Upvotes: 2

Related Questions