java - 使用 SearchDialog Intent 打开 Activity 的 Activity

标签 java android search

我正在我的应用程序中实现一个搜索对话框,我已经为我的 Activity 配置了 Intent 过滤器,并且已经使用 Intent 调用了该过滤器,但同一个 Activity 是我的主要 Activity,我需要处理该 Intent在已经运行的 Activity 上,当事件发生时,将创建 Activity 的新实例,并再次调用 onCreate。

这是我的代码。

public class MainActivity extends Activity {
    private int ht;
    private ImageView img;
    private Bitmap bmp;

    private int width = 612, height = 792;

    public void onCreate(Bundle savedInstance) {
        super.onCreate(savedInstance);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.main);

        img = (ImageView) findViewById(R.id.image01);
        img.setMinimumHeight(height);
        img.setMinimumWidth(width);

        draw();
        refreshImage();

        handleIntent(getIntent());
    }

    protected void onNewIntent(Intent intent) {
        setIntent(intent);
        handleIntent(intent);
    }


    private void search(String text) {
    //Do the search
    }


    private void handleIntent(Intent intent) {
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            String query = intent.getStringExtra(SearchManager.QUERY);
            search(query);
        }
    }
}

和过滤器:

        <activity android:name="MainrActivity"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>
            <meta-data android:name="android.app.searchable"
                android:resource="@xml/searchable" />
        </activity>

searchable.xml

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/app_name"
    android:hint="Search text..." >
</searchable> 

如何处理打开的 Activity 上的此 Intent ?

最佳答案

<activity android:name="MainActivity" 
          android:launchMode="singleTop"

参见http://developer.android.com/guide/topics/search/search-dialog.html

关于java - 使用 SearchDialog Intent 打开 Activity 的 Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4748898/

相关文章:

java - 使用 slf4j 实现自定义记录器

android - React Navigation + Redux 在按下返回时关闭应用程序

在网格上查找和填充封闭形状的算法

java - 球离开屏幕

java - 在不卡住线程的情况下 Ping 服务器

c# - 如何在 SpannableString Android 中设置不透明度/alpha

c# - 哪些算法用于搜索单词中的子词?

php检查字符串中的多个值

java - 让我的线程等待而不阻塞子线程

android - 如何开发响应式网页