我發現了一個令人尷尬的簡單解決方案。
清單:
<activity
android:name=".MainListActivity"
android:label="@string/app_name"
android:launchMode="singleTop" >
<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.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
列表/搜索活動:
public class MainListActivity extends FragmentActivity
implements LoaderManager.LoaderCallbacks<Cursor>
{
private boolean searching = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_cheapest);
dbHelper = new DatabaseHelper(this);
String[] fromColumns = {"pname", "store", "unit_price"};
int[] toViews =
{R.id.entryProductName, R.id.entryStore, R.id.entryUnitPrice};
adapter = new SimpleCursorAdapter(this, R.layout.cheapest_row_entry,
null, fromColumns, toViews);
ListView lv = (ListView)findViewById(android.R.id.list);
lv.setAdapter(adapter);
lv.setEmptyView(findViewById(android.R.id.empty));
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> l, View v, int position,
long id) {
searching = false;
startActivity(new Intent(getBaseContext(),
EditProductActivity.class).putExtra("pid", ""+id));
}
});
handleIntent(getIntent());
}
@Override
public void onBackPressed() {
if (searching) {
searching = false;
getSupportLoaderManager().restartLoader(LOADER_MAIN, null, this);
} else {
finish();
}
}
@Override
protected void onNewIntent(Intent intent) {
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
searching = true;
filter = intent.getStringExtra(SearchManager.QUERY);
getSupportLoaderManager().restartLoader(LOADER_SEARCH, null, this);
} else {
getSupportLoaderManager().initLoader(LOADER_MAIN, null, this);
}
}
...
}
不過,我也第一次得到activity-alias
工作。我將它列入後人。
清單:
<activity
android:name=".MainListActivity"
android:label="@string/app_name"
android:launchMode="singleTop" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.default_searchable"
android:value=".SearchListActivity" />
</activity>
<activity-alias
android:name=".SearchListActivity"
android:launchMode="singleTop"
android:targetActivity=".MainListActivity" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable"
android:value=".SearchListActivity" />
</activity-alias>
列表/搜索活動:
public class MainListActivity extends FragmentActivity
implements LoaderManager.LoaderCallbacks<Cursor>
{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_view);
...
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> l, View v, int position,
long id) {
if (Intent.ACTION_SEARCH.equals(getIntent().getAction())) {
finish();
}
startActivity(new Intent(getBaseContext(),
EditProductActivity.class).putExtra("pid", ""+id));
}
});
handleIntent(getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
filter = intent.getStringExtra(SearchManager.QUERY);
getSupportLoaderManager().restartLoader(LOADER_SEARCH, null, this);
} else {
getSupportLoaderManager().initLoader(LOADER_MAIN, null, this);
}
}
...
}