Why is Android SearchWidget not sending an Intent?

时间:2015-05-04 19:40:46

标签: android android-intent searchview

OK, I've read every tutorial and stuff on this site, and this should be working. I tried doing this with a previous project and got the same results, so its something I'm missing that's more advanced than a typo.

The search widget appears and seems to be working. However, when the search is submitted, nothing happens. It just sits there. I have tried to override onNewIntent() and onSearchRequested() to add Log.d() output to see if the code is ever called. I've added breakpoints there as well ... nothing! That seems to point to how the Intent is set up in the manifest, but it looks right to me. Maybe another pair of eyes can spot something.

manifest.xml Snippet

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:launchMode="singleTop"
    android:theme="@style/AppTheme" >
    <meta-data android:name="android.app.default_searchable"
        android:value=".DeckView" />
    <activity
        android:name=".DeckView"
        android:label="@string/title_activity_deck_view" >
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <meta-data
            android:name="android.app.searchable"
            android:value="@xml/searchable" />
    </activity>

    <activity

xml/searchable.xml

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/search" >
</searchable>

menu/deck_view.xml Note - I have the second search menu calling a dialog of my own which at least lets me search until I get the Android one working. They share actual search-code, just no shared interface.

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:icon="@android:drawable/ic_menu_preferences"
    android:title="@string/action_settings"
    android:showAsAction="ifRoom"/>
<item android:id="@+id/action_dbops" android:title="@string/maintenance"
    android:icon="@android:drawable/ic_menu_more"
    android:orderInCategory="70" android:showAsAction="ifRoom" />

<item android:id="@+id/menu_search" android:title="@string/search"
    android:icon="@android:drawable/ic_menu_search"
    android:actionViewClass="android.widget.SearchView"
    android:showAsAction="collapseActionView|ifRoom" />

<item android:id="@+id/action_search" android:title="@string/search"
    android:icon="@android:drawable/ic_menu_search"
    android:showAsAction="never" />

<item android:id="@+id/action_help" android:title="@string/help"
    android:icon="@android:drawable/ic_menu_help"
    android:orderInCategory="25" android:showAsAction="ifRoom" />
</menu>

Java source functions .DeckView activity Snippets. Note: I have a call in onCreate that also does handleIntent(getIntent());

@Override
public void onNewIntent(Intent intent) {
    Log.d("TRACE", "New Intent!");
    setIntent(intent);
    handleIntent(intent);
}

@Override
public boolean onSearchRequested() {
    Log.d("TRACE", "ONSEARCHREQUESTED");
    Bundle appData = new Bundle();
    appData.putLong("deck",4);
    startSearch(null, false, appData, false);
    return true;
}

private void handleIntent(Intent intent)
{
    if (intent == null)
        return;

        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            long deck;

            Log.d("TRACE","ITS A SEARCH!");
            String query = intent.getStringExtra(SearchManager.QUERY);
            DeckDbHelper dbh = new DeckDbHelper (this,this);
            Bundle appData = getIntent().getBundleExtra(SearchManager.APP_DATA);
            if (appData != null) {
                deck = appData.getLong("deck");
            } else
                deck = 4;
            long id = dbh.createSearchDeck(deck,query);
            Intent newintent = new Intent(this, ShowCard.class);
            intent.putExtra("deckid", String.valueOf(id));
            startActivity(newintent);
        }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the options menu from XML
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.deck_view, menu);

    // Get the SearchView and set the searchable configuration
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
    // Assumes current activity is the searchable activity
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default

    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        Intent intent = new Intent(this, SettingsActivity.class);
        startActivityForResult(intent, 0, null);
    } else if (id == R.id.action_search) {
        search_options();
    } else if (id == R.id.action_dbops) {
        Intent intent = new Intent(this, DatabaseOps.class);
        startActivityForResult(intent, 0, null);
    } else if (id == R.id.action_help) {
        Intent intent = new Intent(this, ShowCard.class);
        intent.putExtra("deckid", String.valueOf(DeckDbHelper.DECK_HELP));
        intent.putExtra("title", "DeckList");
        startActivity(intent);
    }
    setTarget(null);
    return super.onOptionsItemSelected(item);
}

Any help or insight would be greatly appreciated. I don't think I've ever hit a brick wall this solidly, and I don't even know how to debug this!

0 个答案:

没有答案