I am building an app which contains a signle Activity which contains an ActionBar. The activity contains a tab host with 4 tabs and each can be searched for something else, depending on the tab and fragment that is displayed.
According to the developer guide (https://developer.android.com/training/search/setup.html#add-sv), when pressing the search button after the user has entered his query, the activity will be launched with a search intent in which I can read the query like this:
if (Intent.ACTION_SEARCH.equals(intent.getAction()))
{
String query = intent.getStringExtra(SearchManager.QUERY);
//use the query to search your data somehow
}
However, I don't want to relaunch the activity, then decide which fragment raised the query, set the tabs to the correct one, constructing a new fragment and so on... What I want to do is simply read the query itself inside the fragment which the user type in (I noticed that the SearchView class got a getQuery method).
How can I somehow implement a listener that reads the query after the user has finished writing his query and pressed the search button in the virtual keyboard without recreating the activity? I want to just get a reference to the search box and use it like it's a TextView, read it's text and use it.
What I eventually did was implementing OnQueryTextListener. This listener got 2 methods: onQueryTextChange which is called on every key pressed with the text as the parameter, and onQueryTextSubmit which is invoked when the user presses the search button. If you return true in onQueryTextSubmit, it means that everything is handled and there is no need to launch an intent.
Related
I'm trying to develop an app which has two editText fields for entering location. So I made two java files
1.MapsActivity.java(with activity_maps.xml)
2.AutoComplete.java(with autocomplete.xml)
In MapsActivity while clicking the 'from' text field, it will open the AutoComplete activity and after clicking any of the suggestions in it will open a fresh MapsActivity and places the clicked item . Likewise for 'to' text field also.
I am using Intents to pass the clicked values. So at last, five activities are opened when the two text boxes are filled. When I click back button ,it is showing the auto complete activity used the previous time.
Question:
Is any other way to close a particular activity with same name running behind?
Is there any other way to pass values by super.backpressed(); ?
enter image description here
You can try using startActivityForResult() method. That way you can use the result from the second activity in the first and also you wouldn't need to start a new activity. You can read up on it here startActivityForResult
try to clear backstack/clear_top/single_top using the intent.
Just add/set flags in intent.
or
finish activity after startActivity.
or
you can override the onBackPressed method to call the specific activity.
I want to add search history to my SearchView. I found this question. It is rather useful but it looks like I can't handle it this way. First of all I use only fragments in my app while it looks like the solution above requires to create new activity for making search (well maybe I can reuse the same activity using some Intent flags). Also I can't provide search hint bia string resources.
I have fragment with SearchView in actionbar. When user enters search query I just perform web request and display results in the same fragment. I only need to add search history functionality to it. Can this be done completly programmatically?
I'm creating an app where I display a list of pending challenges. When the user clicks on a challenge, he can accept it or ignore it.
Here's what I want to do and I don't know how :
if the user accepts or ignore the challenge, call this.finished and remove the challenge from the list
if the back button is pressed, do nothing, the challenge is still visible
In short, if the user really responds to the challenge I don't want it to be displayed in the list, but if he doesn't choose any option and press the back button, he didn't choses one of the two actions so I want that challenge to still be visible in the list.
I don't think it's possible to detect what button I've pressed when i go back to my main activity. I've thought about using global variables, but I don't want to misuse them either.
Just to be clear, I'm not asking how deleting a list item. But when to know deleting one depending of the actions of another activity.
Give your second activity the index you want to remove as a parameter inside the intent and let it finish by returning the index again as an intent extra (by using setresult(Intent i) and then calling finish) inside your first activity catch the result from your second activity by overwriting onActivityResult (http://developer.android.com/reference/android/app/Activity.html#onActivityResult(int, int, android.content.Intent))
see 3.3. Retrieving result data from a sub-activity in http://www.vogella.com/tutorials/AndroidIntent/article.html for a detailed howTo
My application shall work the following way:
Activity #1 contains a text field (EditText) and a button.
If the user clicks the button a search dialog is opened via onSearchRequested().
This calls the searchable activity #2 which extends ListActivity. It provides a list of items via setListAdapter().
If the user clicks on a list item activity #2 shall pass the selected item's text back to activity #1 and display it in the text field.
Bullets #1-3 are clear and working. However I don't have any idea how to implement #4. I know about the possibility to use intents but it doesn't work if I use an intent after onSearchRequested().
Thanks,
Robert
The following solution is working fine for me:
http://blog.dpdearing.com/2011/05/getting-android-to-call-onactivityresult-after-onsearchrequested/
I would simply send an intent with your selected item as extra (putExtra) to your activity#1 (since the search dialog is between activity#1 and activity#2, you can not use startActivityForResult to post back the result to activity#1)
If the search dialog is in activity#1, then you can use startActivityOnResult (thanks dmon)
Simple, quick fix: store the data statically and do a check to retrieve it in Activity #1's onResume().
I have a question on the Android Activity, for example I have a TabHost, and there are included four Activities, the first tab is a search Activity, enter a keyword in the current result of this Activity to return, and in the current Activity display. Is called to display the search results themselves. And after searching several times, and then return to key mobile phone keypad, the display is the result of the last search keyword, I want the press back key to return to the last call of the Activity or TabHost. Should I do?
By the way, in a tab in the use of Intent calls a Activity,
eg: host.addTab (host.newTabSpec ("friend"). setIndicator ("search")
. SetContent (new Intent (this, Search.class)));
In this Activity in the need to call another Activity,
e.g: startActivity (new Intent (this, Other.class));
Also called another Activity displayed on this tab, but not yet jump out of the show. I ask how you can achieve this?
First, you can use startActivityForResult(...) instead of startActivity(...). This means that when you're done with the activity, you want to come back to the activity that started it so you can do more.
Second, you can override the onKeyDown(...) method and define whatever behavior you want for the back key. however, this is not recommended by Google except when absolutely necessary.