2011-03-11 34 views
1

我實現了Google的Searchable Dictionary版本,我遇到了一些問題。保持Android MapView搜索組件不會產生新的Intent

我在一個MapView活動中實現了一個基本的ItemizedOverlay,我想要搜索。我已經將數據替換爲與我的ItemizedOverlay相同,並且所有內容似乎都可以很好地查詢。

但是,當我從列表中選擇一個搜索項目時,產生搜索活動的原始意圖似乎處理該事件,並且新的MapView意圖會產生並執行完全相同的操作(所以如果我按下「返回」按鈕,我回到重複的mapView)。

我的MapView類看起來是這樣的:

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

    setContentView(R.layout.my_map); 

    initializeComponents(); 

    addOverlays(); 

    Intent intent = getIntent(); 

    if (Intent.ACTION_VIEW.equals(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(); 
    } 
} 

瘋狂的事情是Toast通知工作得很好 - 它給了我點的緯度/經度(是的,我知道列的索引是不最好的方式來檢索它)。關於爲什麼創建第二個MapView意圖的任何想法?

在情況下,如果你需要它,這裏的searchable.xml文件:

<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" 
    > 

回答

0

好,知道了想通了。文檔中明確指出

默認情況下,可搜索Activity通過調用onCreate()來接收ACTION_SEARCH Intent,並將Activity的新實例引入Activity堆棧的頂部。在Activity堆棧中現在有兩個可搜索的Activity實例(所以按BACK鍵可返回到可搜索Activity的前一個實例,而不是退出可搜索的Activity)。

如果你設置的android:launchMode到 「singleTop」,那麼搜索 活動收到ACTION_SEARCH 意圖一起 onNewIntent(意向)的調用,路過此處的新 ACTION_SEARCH意圖。

我所要做的就是重寫onNewIntent方法來處理搜索事件。現在一切正常!