update the SearchableDictionary sample to use SearchView in the Action Bar
and set the main activity to use launchMode="singleTop" Change-Id: I72bd13bb8250d0b00a713fe2980a5ad692f2cb9e
This commit is contained in:
@@ -21,14 +21,14 @@
|
||||
android:versionCode="2"
|
||||
android:versionName="2.0">
|
||||
|
||||
<uses-sdk android:minSdkVersion="4" />
|
||||
<uses-sdk android:minSdkVersion="11" />
|
||||
|
||||
<application android:label="@string/app_name"
|
||||
android:icon="@drawable/ic_dictionary">
|
||||
|
||||
<!-- The default activity of the app; displays search results. -->
|
||||
<activity android:name=".SearchableDictionary"
|
||||
android:theme="@android:style/Theme.NoTitleBar">
|
||||
android:launchMode="singleTop">
|
||||
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
@@ -48,8 +48,7 @@
|
||||
</activity>
|
||||
|
||||
<!-- Displays the definition of a word. -->
|
||||
<activity android:name=".WordActivity"
|
||||
android:theme="@android:style/Theme.NoTitleBar" />
|
||||
<activity android:name=".WordActivity" />
|
||||
|
||||
<!-- Provides search suggestions for words and their definitions. -->
|
||||
<provider android:name=".DictionaryProvider"
|
||||
|
||||
@@ -20,5 +20,7 @@
|
||||
<menu xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<item android:id="@+id/search"
|
||||
android:title="@string/menu_search"
|
||||
android:icon="@drawable/ic_menu_search" />
|
||||
android:icon="@drawable/ic_menu_search"
|
||||
android:showAsAction="ifRoom"
|
||||
android:actionViewClass="android.widget.SearchView" />
|
||||
</menu>
|
||||
|
||||
@@ -34,7 +34,7 @@
|
||||
<string name="settings_description">Definitions of words</string>
|
||||
|
||||
<!-- General instructions in the main activity. -->
|
||||
<string name="search_instructions">Press the search key to look up a word</string>
|
||||
<string name="search_instructions">Use the search box in the Action Bar to look up a word</string>
|
||||
|
||||
<!-- Shown above search results when we receive a search request. -->
|
||||
<plurals name="search_results">
|
||||
|
||||
@@ -17,7 +17,9 @@
|
||||
package com.example.android.searchabledict;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.ActionBar;
|
||||
import android.app.SearchManager;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.database.Cursor;
|
||||
import android.net.Uri;
|
||||
@@ -28,6 +30,7 @@ import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.ListView;
|
||||
import android.widget.SearchView;
|
||||
import android.widget.SimpleCursorAdapter;
|
||||
import android.widget.TextView;
|
||||
import android.widget.AdapterView.OnItemClickListener;
|
||||
@@ -50,8 +53,19 @@ public class SearchableDictionary extends Activity {
|
||||
mTextView = (TextView) findViewById(R.id.text);
|
||||
mListView = (ListView) findViewById(R.id.list);
|
||||
|
||||
Intent intent = getIntent();
|
||||
handleIntent(getIntent());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onNewIntent(Intent intent) {
|
||||
// Because this activity has set launchMode="singleTop", the system calls this method
|
||||
// to deliver the intent if this actvity is currently the foreground activity when
|
||||
// invoked again (when the user executes a search from this activity, we don't create
|
||||
// a new instance of this activity, so the system delivers the search intent here)
|
||||
handleIntent(intent);
|
||||
}
|
||||
|
||||
private void handleIntent(Intent intent) {
|
||||
if (Intent.ACTION_VIEW.equals(intent.getAction())) {
|
||||
// handles a click on a search suggestion; launches activity to show word
|
||||
Intent wordIntent = new Intent(this, WordActivity.class);
|
||||
@@ -115,6 +129,12 @@ public class SearchableDictionary extends Activity {
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
MenuInflater inflater = getMenuInflater();
|
||||
inflater.inflate(R.menu.options_menu, menu);
|
||||
|
||||
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
|
||||
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
|
||||
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
|
||||
searchView.setIconifiedByDefault(false);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -17,6 +17,8 @@
|
||||
package com.example.android.searchabledict;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.ActionBar;
|
||||
import android.content.Intent;
|
||||
import android.database.Cursor;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
@@ -35,6 +37,9 @@ public class WordActivity extends Activity {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.word);
|
||||
|
||||
ActionBar actionBar = getActionBar();
|
||||
actionBar.setDisplayHomeAsUpEnabled(true);
|
||||
|
||||
Uri uri = getIntent().getData();
|
||||
Cursor cursor = managedQuery(uri, null, null, null, null);
|
||||
|
||||
@@ -67,6 +72,11 @@ public class WordActivity extends Activity {
|
||||
case R.id.search:
|
||||
onSearchRequested();
|
||||
return true;
|
||||
case android.R.id.home:
|
||||
Intent intent = new Intent(this, SearchableDictionary.class);
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
||||
startActivity(intent);
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user