5

我有一个活动,我通常希望在多个任务中存在,以便 [Back] 按钮恢复以前的状态;但是,我还想将SearchView与现有活动一起使用,而不将新活动推入任务堆栈(因为我想搜索当前显示的内容)。这是我的问题:

  • 如果我将活动的启动模式设置为“singleTop”,SearchView 将与当前活动一起使用,但Context.startActivity不会将新活动添加到堆栈中。
  • 如果我将活动的启动模式设置为“标准”,则startActivity会将新活动添加到堆栈中,但 SearchView 还会创建一个新的(空)活动,而不是搜索现有活动。

我找不到任何方法让 SearchView 使用意图标志(例如FLAG_ACTIVITY_SINGLE_TOP。我尝试反过来,将启动模式设置为“singleTop”,然后将 FLAG_ACTIVITY_NEW_TASK 添加每个意图,但这仅在从不同的活动类启动时有效;当我尝试从它自己的类中启动一个新的活动实例时,Android 不尊重 FLAG_ACTIVITY_NEW_TASK 并且没有将新任务推送到堆栈上。

也许我可以在Activity.onNewIntent中做一些事情来强制活动克隆自己。

我即将放弃 SearchView 并只是编写自己的自定义小部件,尽管“从 Android 3.0 开始,使用 SearchView 小部件作为操作栏中的项目是在您的应用中提供搜索的首选方式"来源)- 到目前为止,它似乎太不灵活且不可配置,无法在非平凡的情况下使用。

有没有人找到解决这个问题的方法?

4

2 回答 2

4

经过大量的反思和错误的开始,我意识到 SearchView 对这项任务来说是完全错误的。Google 在设计 SearchView 时考虑了一种非常特定类型的搜索活动:

  • 用户键入搜索查询。
  • 该应用程序推送一个显示结果列表的新活动。

那不是我使用的搜索模型;相反,我想使用查询细化当前显示在屏幕上的列表——它更像是一种过滤搜索,而不是同步查询和响应模型。SearchView 不是为此而设计的,所以我继续编写自己的小部件(并将其绑定到搜索菜单项和搜索按钮)。

我很惊讶没有人对这个问题发表任何评论——通常 Android 问题会引起很多讨论。我想这离那里太远了。

于 2013-02-23T23:09:11.057 回答
2

我遇到了与 OP 相同的问题,但有一个解决方案并不意味着放弃使用SearchView. 这不是完美的解决方案,但它相当干净并且似乎运行良好。

正如OP所说...

当我尝试从它自己的类中启动一个新的活动实例时,Android 不尊重 FLAG_ACTIVITY_NEW_TASK 并且没有将新任务推送到堆栈上

...所以我的解决方案是通过中介活动。为此,我创建了一个DummyActivity类,它基本上采用原始意图并将其转发到您的目标活动。

所以,从我原来的活动中,而不是打电话...

Intent destinationIntent = new Intent(getActivity(), DestinationActivity.class);
startActivity(destinationIntent);

...我打电话...

/*
 * Create the intent as normal, but replace our destination Activity with DummyActivity
 */
Intent dummyIntent = new Intent(getActivity(), DummyActivity.class);

/*
 * This will make it so that if user clicks back from the next activity, they won't be shown DummyActivity
 */
dummyIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

/*
 * Tell DummyActivity about the destination Activity
 */
dummyIntent.putExtra(DummyActivity.EXTRA_DESTINATION_ACTIVITY, YourDestinationActivity.class); //IMPORTANT - the destination Activity must implement Serializable

/*
 * If you want, you can add any other extras to dummyIntent (which will be passed through to the destination activity). Launch DummyActivity.
 */
startActivity(dummyIntent);

在我的清单中,DesinationActivity 被定义为 singleTop。

你应该需要的唯一另一件事是 DummyActivity 类,它不需要任何主要的定制......

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class DummyActivity extends Activity {

    public static final String EXTRA_DESTINATION_ACTIVITY = "uk.co.mobilewebexpert.EXTRA_DESTINATION_ACTIVITY";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_dummy);

        Intent intent = getIntent();

        /*
         * Get destination activity info
         */
        @SuppressWarnings("unchecked")
        Class<Activity> destinationActivity = (Class<Activity>) intent.getSerializableExtra(EXTRA_DESTINATION_ACTIVITY);
        Bundle destinationActivityExtras = intent.getExtras();

        /*
         * Launch destination activity
         */
        Intent destinationActivityIntent = new Intent(this, destinationActivity);
        destinationActivityIntent.putExtras(destinationActivityExtras);
        destinationActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(destinationActivityIntent);
    }

    /**
     * Override super.onNewIntent() so that calls to getIntent() will return the latest
     * intent, rather than the first intent.
     */
    @Override
    public void onNewIntent(Intent intent){
        super.onNewIntent(intent);
        setIntent(intent);
    }

}

希望这也对你有用。欢迎提出建议。干杯。

于 2014-11-25T11:13:06.667 回答