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

1 回答 1

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);
        }
    }
    ...
}
于 2012-08-19T15:28:14.357 回答