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:
@@ -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