Two searchable.xml activities in one AndroidManifest.xml - android

I have an Android app which has a few different activities for browsing articles and images downloaded from RSS.
I'd like to be able to offer to hook up the search button to the Search dialog, using the a searchable.xml file. I've managed to do this with one search, using:
<activity android:name=".search.SearchResultsActivity"
android:label="#string/search_results_activity_title" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable_articles"/>
</activity>
and in the <application />
<meta-data android:name="android.app.default_searchable"
android:value=".search.SearchResultsActivity" />
I can now launch the Search dialog from any activity, and it launches the SearchResultsActivity.
I would now like to be able to search for images when the user is an ImageListActivity, using a searchable_images.xml, and use the default everywhere else.
I have a SearchResultsImageActivity which includes the following meta-data element, and used the same element in the ImageListActivity.
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable_images"/>
On pressing the search button in the ImageListActivity, I get the default search from searchable_articles.xml.
If I change the default_searchable to SearchResultsImageActivity, the image search is always launched, and the article search is never launched.
If I remove the default_searchable meta-data element altogether, and add searchable meta-data only selected activities, no search is launched.
I'm fairly sure this should be possible, but I don't know what I'm doing wrong.

In your Manifest file update the ImageListActivity activity tag
<activity
android:name=".ImageListActivity"
...
<meta-data
android:name="android.app.default_searchable"
android:value=".SearchResultsImageActivity" />
</activity>
So when you will trigger native search in ImageListActivity it will invoke the SearchResultsImageActivity and default one for others.
Assuming SearchResultsImageActivity is searchable.

One way I did this was to create fake activities then switch out the activities when you need them.
<activity android:name="activitySearchMain" />
<activity android:name="activitySearchSub1">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.ALTERNATIVE" />
<category android:name="android.intent.category.SELECTED_ALTERNATIVE" />
<data android:scheme="user" />
</intent-filter>
</activity>
<activity android:name="activitySearchSub2">
<intent-filter>
<action android:name="com.sample.twitter.action.SEARCH" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.ALTERNATIVE" />
<category android:name="android.intent.category.SELECTED_ALTERNATIVE" />
<data android:scheme="user" />
</intent-filter>
</activity>
Create two class that are named for the sub activities.
then create intents like this when component is clicked...
Intent sourceIntent = getIntent();
Intent newIntent = new Intent(this, activitySearchSub2.class);
newIntent.setAction(activitySearchSub2.ACTION2);
newIntent.setData(sourceIntent.getData());
startActivity(newIntent);
finish();
and call the intents from onClick when a button is clicked or some other component is click:

If you override the search function in just that activity, it should prevent the search call from going up to the application level. The return value controls if the call is propagated up.
#Override
public boolean onSearchRequested() {
onPromptSearch();
return false; // don't go ahead and show the search box
}

Related

Receive data intent opens second app when app is opened

<activity
android:name=".activitys.SplashScreen"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.MAIN" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity>
When app is sterted, trying to share the text and app is launching but if open pervious app by android navigation button i see 2 my app exemplars. How to check if app is launched and reopen not open second at one time
Simple answer: you need to add
android:launchMode="singleInstance"
within your activity bracket (below android:name=".activitys.SplashScreen", for example)
Better answer: Go through this article to understand the different types of launch modes: https://medium.com/#iammert/android-launchmode-visualized-8843fc833dbe
Best answer: Read through the doc on tasks: https://developer.android.com/guide/components/activities/tasks-and-back-stack

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.

Adding <data> tag to Intent-Filter in Action not being honored

I've searched documentation, but I'm missing something obvious. Or am trying to do something backwards.
What I have is the main activity, that's fired from the launcher and widgets. I have a second activity that I'd like to be fired off when a URL of a specific pattern is attempted to be opened. Here's the two activity definitions:
<activity android:name=".activities.MainMapScreen" android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".activities.ViewDatasheet" android:configChanges="orientation|keyboardHidden" android:label="#string/app_name" android:process=":BMMapsDatasheet">
<meta-data android:name="Main Screen" android:value=".activities.MainMapScreen" />
<intent-filter>
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http" android:host="www.ngs.noaa.gov" android:pathPattern="\\/cgi-bin\\/ds_mark.prl\\?PidBox\\=([a-zA-Z]{2}[0-9]{4})" />
</intent-filter>
</activity>
Like the above, url's aren't intercepted. Even if I have nothing in the data but the scheme and host, I don't get the prompt to select my app.
As a test, I copied the browsable and to the main activity, and when I do that, the icon disappears form the launcher altogether. (And I still get no prompt when trying to hit the URL.
All the examples I find has the in the main activity, not a secondary activity, so.. I'm not sure if I'm missing some flags or such.
Any thoughts would be appreciated. If you want me to post the whole Manifest, let me know.
--Mike.
Edit: After adding #iturki suggestions, there was no change. however - if I also added <category android:name="android.intent.category.OPENABLE" /> to the action to handle the URLs, that worked. None of the examples I saw with the had that category, so I'm not yet sure if it would affect other operations of the activities in my application or not.
Try adding a VIEW action to your second Activity's <intent-filter>:
<action android:name="android.intent.action.VIEW"></action>
And I think you might want to add your Intent to the DEFAULT category too:
<category android:name="android.intent.category.DEFAULT"></category>
I'm not sure if it is important but all the <intent-filter>s have this category.

Search Interface opens a new Activity when search

I am trying to use the Search Manager from Android.
In fact, I have an activity where I am calling
onSearchRequested()
Then, I am using in the same activity this function to get the search string:
// Get the intent, verify the action and get the query
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
}
The problem is the next: When i am clicking to the search button, I am opening a new activity and i would like to stay on the same and do some searches. So, my goal is to avoid the new activity opening when I click to the search button.
Thank you.
The solution consists to reuse the developpers example http://developer.android.com/guide/topics/search/search-dialog.html
The important thing is to create 2 activities, one for searches and one other for displaying the results.
In the manifest, use this code:
<application ... >
<!-- this is the searchable activity; it performs searches -->
<activity android:name=".SearchableActivity" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable"/>
</activity>
<!-- this activity enables the search dialog to initiate searches
in the SearchableActivity -->
<activity android:name=".OtherActivity" ... >
<!-- enable the search dialog to send searches to SearchableActivity -->
<meta-data android:name="android.app.default_searchable"
android:value=".SearchableActivity" />
</activity>
...
use in Manifest
<activity android:name="BrowseItems" android:label="#string/browseitems"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/itemsearchable" />
</activity>

Howto intercept intents (for calendar event creation)

I would like to create a custom way to enter new events to the calendar.
From what I understood it should be easy by creating an app responding to the intent being sent by the calendar app. I created the following activity in my app:
<activity android:name=".Main"
android:label="#string/app_name">
<!-- for normal launch -->
<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.EDIT" />
<category android:name="android.intent.category.ALTERNATIVE" />
<!-- also tried DEFAULT and others -->
<data android:mimeType="vnd.android.cursor.item/event" />
</intent-filter>
</activity>
But still only the builtin dialog pops up. From what I've seen in the sources of the calendar app the Intent isn't hardcoded there.
Am I missing something obvious?
After some more digging in the source code I found out that the intent target class is hardcoded. So no way of overriding the event edit activity. ;-(

Categories

Resources