按下后退按钮时,singleTop可搜索活动终止

时间:2012-08-17 15:21:23

标签: android android-intent android-manifest

我有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 个答案:

答案 0 :(得分: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);
        }
    }
    ...
}