2012-08-17 94 views
1

我有一個ListActivity作爲我的應用程序的主要活動。列表中的項目應該是可搜索的,但自然我不想一次又一次地開始同一活動,因此singleTop。但是,我確實希望用戶能夠在搜索後返回主列表,這給我帶來了一些麻煩。目前,搜索後按回來將終止該應用程序。一種解決方案是專門製作一份列表活動的副本來處理搜索,但我無法想象這是最好的解決方法。我簡要地嘗試了activity-alias,但是我嘗試過的每個置換都不會改變行爲或改爲執行Web搜索。我不知道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> 
</activity> 
<activity-alias 
    android:name=".SearchList" 
    android:targetActivity=".MainListActivity" 
    android:launchMode="singleTop"> 
    <intent-filter> 
     <action android:name="android.intent.action.SEARCH" /> 
    </intent-filter> 
    <meta-data 
     android:name="android.app.searchable" 
     android:resource="@xml/searchable" /> 
</activity-alias> 

回答

1

我發現了一個令人尷尬的簡單解決方案。

清單:

<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); 
     } 
    } 
    ... 
} 
相關問題