Intent filter works only once - android

I have an activity called "MainActivity", this is my main activity which declared in the manifest file as launcher:
<activity
android:name="com.example.tester.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VOICE_COMMAND" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
The VOICE_COMMAND works only one time and then nothing is happen.
I have tried to separate it into 2 activities: 1 activity is the main and the other is associated with the VOICE_COMMAND intent filter, and still, the application works only once.
If it's separated into 2 activities it works fine only when the application starts immediately from the launcher, but if the application starts from the second activity (with the VOICE_COMMAND intent filter) it works only once.
Any suggestion?
Thank you

I have found the answer:
Setting the attribute:
android:launchMode="singleTask"
and it works fine.
Thank you

Related

singleTask launch mode is not working

Having this weird problem of multiple instances of my activity getting created even after specifying the launchMode as singleTask. Has anybody faced this issue ? Any pointers will be appreciated.
Below is how i have declared my activity in the manifest:
<activity
android:name="com.test.TestActivity"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<action android:name="android.intent.action.SEND_MULTIPLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="*/*" />
</intent-filter>
</activity>
And this is how i am launching my activity while it is still in foreground:
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, sharedText);
sendIntent.setType("text/plain");
activity.startActivity(Intent.createChooser(sendIntent, "Share"));
Android version that i am testing it on is 5.1.1
Try using singleInstance if you want to get rid of your activity's multiple instances.
<activity
android:name=".MainActivity"
android:launchMode="singleInstance">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
EXTRA NOTE:
singleTask :- A new task will always be created and a new instance will be pushed to the task as the root. However, if any activity
instance exists in any tasks, the system routes the intent to that
activity instance through the onNewIntent() method call. In this mode,
activity instances can be pushed to the same task. This mode is useful
for activities that act as the entry points.
singleInstance:- Same as singleTask, except that the no activities instance can be pushed into the same task of the singleInstance’s.
Accordingly, the activity with launch mode is always in a single
activity instance task. This is a very specialized mode and should
only be used in applications that are implemented entirely as one
activity.
Copied from : https://stackoverflow.com/a/36520016/3669559
So it seems the issue is with device. On emulator and other devices with same android version singleTask works just fine.

How to start specific activity by saying OK Google?

I would like to have two activities. FirstActivity should be started from
launcher (by tapping on icon) and SecondActivity should be started by voice
command (by saying "OK Google, start play example").
The problem is that for my current configuration only FirstActivity is
started. Also method isVoiceInteraction returns false. I also don't see any values in flags that indicates that activity was started by voice.
Here's part of AndroidManifest.xml:
<application android:label="play example">
<activity android:name="com.example.FirstActivity" android:label="play example">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="com.example.EXAMPLE_ACTION" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name="com.example.SecondActivity" android:label="play example">
<intent-filter>
<action android:name="com.example.EXAMPLE_ACTION" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.VOICE" />
</intent-filter>
</activity>
</application>
Here it states:
To specify the text to say after "Start", specify a label attribute for the activtiy that you want to start.
But when I change label for SecondActivity it does not help.
I'm using API version 26.
How to setup configuration to handle described behavior?
AFAIK isVoiceInteraction will always return false when the application is opened using "open" voice command. However, the scenario which you described, having another activity to respond to voice commands, should've worked.
The only thing that you might have missed is to put the action android.intent.action.MAIN in the second activity.

Multi icon in screen Android [duplicate]

I am writing an Android App that has one main activity and one subactivity. When I install the app on my phone to test it out, I get two icons instead of one. The first icon is for the main activity and the second is for the subactivity. I don't want/need an icon for the subactivity.
Does anyone know how to turn this off in my app code, so that only the icon for the main activity is installed? Any information you can provide is greatly appreciated!
Thanks,
MobiKnow
Does your application manifest list an intent filter under your sub-activity that matches the main launcher?
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
Make sure that your sub-activity is not filtering for these intents.
Edit: Just to be very clear, make sure the above lines are not listed under your sub-activity. That intent filter lets the Android system know that you intend for it to be listed as an entry point to your application.
We have the same problem but i fixed it this way
before my code below in manifest
<application
android:debuggable="true"
android:allowBackup="true">
<activity android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.noupdate.apptest_noupdate.MainActivity"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Notice that in the SplashActivity inside the intent is this code
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
i only deleted the category
<category android:name="android.intent.category.LAUNCHER" />
So after i deleted the intent-filter category in splash it didn't install two app icon but only one for main the code will be like this notice that the intent-filter category is deleted
<application
android:debuggable="true"
android:allowBackup="true">
<activity android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
<activity
android:name="com.noupdate.apptest_noupdate.MainActivity"
android:icon="#drawable/ic_launcher"
android:theme="#android:style/Theme.NoTitleBar"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
it really helps
It creates two App icon because you must have added the given filter to two of your activities. See manifest.
<category android:name="android.intent.category.MAIN" />
Remove the above statement from the other one. Then you are good to go.
Like in the other answers,
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"/>
Was the culprit. However, in my manifest I only had one activity with that intent filter. As it turns out, I am using a library I built and it has an activity declared in it's manifest which uses that intent filter. So, in short, be sure your app's manifest and dependencies, if any, only have one activity with the intent filter.
I guess that in your AndroidManifest.xml, you've got both activities having the LAUNCHER intent-filter. Remove it from the second activity, and you should be set!
You have
<intent-filter . . . >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
along with
android:icon="#drawable/icon.png"
set for both activities.
What that means is that this is a launcher icon, put me on the home screen. Only set those for the activit(ies/y) you want on the home screen.
I was searching answer to exactly the same question. It appears the only thing needed (in addition to recommendations on removing MAIN and LAUNCHER intent filter) is to rebuild your project - that will clean things up and upon next launch I saw single icon on my device (just running application on device after changes did not help).
if anyone run into this issue using Pebble SDK. I noticed PebbleKit Androidmanifest.xml holds a LAUNCHER activity as well. This is what caused it for me. Just remove this part. It will not effect Pebble functionality.
SIMPLE answer..
REMOVE:
<category android:name="android.intent.category.LAUNCHER" />
From your AndroidManifest.xml
Leave your intent-filter alone.
<intent-filter android:icon=”drawable resource” android:priority=”Integer” /intent-filter>
Priorities are set for the parent component. This setting may help you to set the parent icon for your Android app development.

android) can i set a default activity that runs right after installing

in my app, there is two activity and i want to make activity1 to the starting activity after installation.
But now the RUN button (is showed right after packgae installing) is disabled.
below is the manifest file. thanks.
<activity ...1>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity ...2>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
</intent-filter>
</activity>
android) can i set a default activity that runs right after installing
No activity "runs right after installing". The user has to launch it from the launcher.
below is the manifest file
No, it is not. That is not even valid XML.
Also, note that your third <intent-filter> is invalid. Not only are you missing any category (you need at least DEFAULT for activities), but ACTION_POWER_CONNECTED and ACTION_POWER_DISCONNECTED are not activity actions.
I am going to take a guess that you really mean to ask: "I have two activities, both described as ACTION_MAIN/CATEGORY_LAUNCHER, and now the Run button does not work -- what can I do?" The answer would be "either remove the ACTION_MAIN/CATEGORY_LAUNCHER <intent-filter> from one of them, or mark one of the two as disabled (android:enabled="false") and enable it later on using PackageManager."
I think the problem is the second:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
You shouldn't have 2 activities flagged as the MAIN and LAUNCHER activity.
Try removing it within activity2.
Check out: http://developer.android.com/reference/android/content/Intent.html the intentfilter s are discussed.

Android App Development: Two icons getting created and I only need one

I am writing an Android App that has one main activity and one subactivity. When I install the app on my phone to test it out, I get two icons instead of one. The first icon is for the main activity and the second is for the subactivity. I don't want/need an icon for the subactivity.
Does anyone know how to turn this off in my app code, so that only the icon for the main activity is installed? Any information you can provide is greatly appreciated!
Thanks,
MobiKnow
Does your application manifest list an intent filter under your sub-activity that matches the main launcher?
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
Make sure that your sub-activity is not filtering for these intents.
Edit: Just to be very clear, make sure the above lines are not listed under your sub-activity. That intent filter lets the Android system know that you intend for it to be listed as an entry point to your application.
We have the same problem but i fixed it this way
before my code below in manifest
<application
android:debuggable="true"
android:allowBackup="true">
<activity android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.noupdate.apptest_noupdate.MainActivity"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Notice that in the SplashActivity inside the intent is this code
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
i only deleted the category
<category android:name="android.intent.category.LAUNCHER" />
So after i deleted the intent-filter category in splash it didn't install two app icon but only one for main the code will be like this notice that the intent-filter category is deleted
<application
android:debuggable="true"
android:allowBackup="true">
<activity android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
<activity
android:name="com.noupdate.apptest_noupdate.MainActivity"
android:icon="#drawable/ic_launcher"
android:theme="#android:style/Theme.NoTitleBar"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
it really helps
It creates two App icon because you must have added the given filter to two of your activities. See manifest.
<category android:name="android.intent.category.MAIN" />
Remove the above statement from the other one. Then you are good to go.
Like in the other answers,
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"/>
Was the culprit. However, in my manifest I only had one activity with that intent filter. As it turns out, I am using a library I built and it has an activity declared in it's manifest which uses that intent filter. So, in short, be sure your app's manifest and dependencies, if any, only have one activity with the intent filter.
I guess that in your AndroidManifest.xml, you've got both activities having the LAUNCHER intent-filter. Remove it from the second activity, and you should be set!
You have
<intent-filter . . . >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
along with
android:icon="#drawable/icon.png"
set for both activities.
What that means is that this is a launcher icon, put me on the home screen. Only set those for the activit(ies/y) you want on the home screen.
I was searching answer to exactly the same question. It appears the only thing needed (in addition to recommendations on removing MAIN and LAUNCHER intent filter) is to rebuild your project - that will clean things up and upon next launch I saw single icon on my device (just running application on device after changes did not help).
if anyone run into this issue using Pebble SDK. I noticed PebbleKit Androidmanifest.xml holds a LAUNCHER activity as well. This is what caused it for me. Just remove this part. It will not effect Pebble functionality.
SIMPLE answer..
REMOVE:
<category android:name="android.intent.category.LAUNCHER" />
From your AndroidManifest.xml
Leave your intent-filter alone.
<intent-filter android:icon=”drawable resource” android:priority=”Integer” /intent-filter>
Priorities are set for the parent component. This setting may help you to set the parent icon for your Android app development.

Categories

Resources