开发者

keep Android MapView Search component from spawning new Intent

I implemented a version of Google's Searchable Dictionary, and I'm having some problems.

I'm implementing in a MapView activity in which I have a basic ItemizedOverlay that I want to search. I've replaced the data to be the same as my ItemizedOverlay, and everything seems to query just fine.

However, when I select a search item from the list, the original intent that spawned the search activity appears to handle the event, and a new MapView intent spawns and does the exact same thing (so if I were to press the "back" button, I go back to a duplicate mapView).

My Mapview class looks like this:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.my_map);

    initializeComponents();

    addOverlays();

    Intent intent = getIntent();

    if (Intent.ACTION_VIEW.equal开发者_开发知识库s(intent.getAction())) {
        // handles a click on a search suggestion; launches activity to show word
        int lat, lon;
        Uri uri = intent.getData();
        Cursor c = getContentResolver().query(uri, null, null, null, null);
        try {
            c.moveToFirst();
            lat = Integer.parseInt(c.getString(2));
            lon = Integer.parseInt(c.getString(6));
        } finally {
            c.close();
        }

        //this is the ItemizedOverlay that holds the building icons
        buildingIcons.onTap(new GeoPoint(lat, lon), mapView);

        Toast.makeText(this, "you searched for " + lat + ", " + lon, Toast.LENGTH_LONG).show();
    } 
}

The crazy thing is that the Toast notification works just fine - it gives me the latitude/longitude of the point (and yes I know column index is not the best way to retrieve it). Any ideas on why a second MapView intent is created?

In case if you need it, here's the searchable.xml file:

<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/search_label"
    android:hint="@string/search_hint"
    android:searchSettingsDescription="@string/settings_description"
    android:searchSuggestAuthority=".map.DictionaryProvider"
    android:searchSuggestIntentAction="android.intent.action.VIEW"
    android:searchSuggestIntentData="content://.map.DictionaryProvider/dictionary"
    android:searchSuggestSelection=" ?"
    android:searchSuggestThreshold="1"
    android:includeInGlobalSearch="true"
    >


Okay, got it figured out. The documentation clearly states

By default, the searchable Activity receives the ACTION_SEARCH Intent with a call to onCreate() and a new instance of the Activity is brought to the top of the Activity stack. There are now two instances of your searchable Activity in the Activity stack (so pressing the BACK key goes back to the previous instance of the searchable Activity, rather than exiting the searchable Activity).

If you set android:launchMode to "singleTop", then the searchable Activity receives the ACTION_SEARCH Intent with a call to onNewIntent(Intent), passing the new ACTION_SEARCH Intent here.

What I had to do was override the onNewIntent method to handle the search event. Everything works fine now!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜