检索的活动被称为两次 [英] Searchable activity being called twice

查看:112
本文介绍了检索的活动被称为两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创造我的第一个Android应用程序和增加它的搜索功能的过程。我已经试过了Android开发者文档,同时添加了搜索对话框和窗口小部件。不幸的是,每当我执行搜索时,的onCreate和onNewIntent的搜索活动中被调用。也就是说,通过输入一些在操作栏搜索框,并按下Enter键,搜索被调用两次。我坚持。假定一些全局标志被从检索的活动通知该应用返回的搜索已经完成?现被称为无论是搜索对话框和Widget?

我搜索这个网站和网络无济于事的previous职位。感谢您的任何帮助。

AndroidManifest.xml中

 < XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=com.shop
    安卓版code =1
    机器人:VERSIONNAME =1.0>

    <用途-SDK
        安卓的minSdkVersion =14
        机器人:targetSdkVersion =17/>

    <使用-权限的Andr​​oid:名称=android.permission.INTERNET对/>

    <应用
        机器人:allowBackup =真
        机器人:图标=@可绘制/ ic_launcher
        机器人:标签=@字符串/ APP_NAME
        机器人:主题=@风格/ AppTheme>

        <服务机器人:名称=com.shop.RestIntentService/>

        <活动
            机器人:名称=com.shop.MainActivity
            机器人:标签=@字符串/ APP_NAME>
            <意向滤光器>
                <作用机器人:名称=android.intent.action.MAIN/>

                <类机器人:名称=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
        < /活性GT;

        <活动
            机器人:名称=com.shop.CatalogActivity
            机器人:标签=@字符串/ APP_NAME>
        < /活性GT;

        <活动
            机器人:名称=com.shop.SearchableActivity
            机器人:launchMode =singleTop>
            <意向滤光器>
                <作用机器人:名称=android.intent.action.SEARCH/>
            &所述; /意图滤光器>

            <! - 
            <意向滤光器>
                <作用机器人:名称=android.intent.action.VIEW/>
            &所述; /意图滤光器>
             - >

            &所述;元数据
                机器人:名称=android.app.searchable
                机器人:资源=@ XML /搜索/>
        < /活性GT;

        &所述;元数据
            机器人:名称=android.app.default_searchable
            机器人:值=com.shop.SearchableActivity/>

    < /用途>

< /舱单>
 

SearchableActivity.java

 公共类SearchableActivity扩展ListActivity实现接收器{

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的System.out.println(的onCreate);
        handleIntent(getIntent());
    }

    @覆盖
    公共无效onNewIntent(意向意图){
        super.onNewIntent(意向);
        的System.out.println(onNewIntent);
        setIntent(意向);
        handleIntent(意向);
    }

    私人无效handleIntent(意向意图){
        如果(Intent.ACTION_SEARCH.equals(intent.getAction())){
            查询字符串= intent.getStringExtra(SearchManager.QUERY);
            doSearch(查询);
        }
    }

    私人无效doSearch(字符串queryStr){
        的System.out.println(搜索...+ queryStr);
    }
 

CatalogActivity.java

  @覆盖
    公共布尔onCreateOptionsMenu(功能菜单){
        MenuInflater充气= getMenuInflater();
        inflater.inflate(R.menu.sample,菜单);

        SearchManager searchManager =(SearchManager)getSystemService(Context.SEARCH_SERVICE);

        搜索查看搜索查看=(搜索查看)menu.findItem(R.id.menu_search).getActionView();

        sea​​rchView.setSearchableInfo(searchManager
                .getSearchableInfo(getComponentName()));

        //返回super.onCreateOptionsMenu(菜单);
        返回true;
    }
 

sample.xml中

 <项目
    机器人:ID =@ + ID / menu_search
    机器人:actionViewClass =android.widget.SearchView
    机器人:图标=@机器人:可绘制/ ic_menu_search
    机器人:showAsAction =ifRoom | collapseActionView
    机器人:标题=@字符串/ menu_search/>
 

解决方案

我觉得方法onNewIntent和的onCreate是互斥的。设置launchMode为singleTop,因此该方法onNewIntent将被称为不和所述的onCreate

我也有类似的问题,当一个搜索请求被处理两次。与在WXGA 10.1平板电脑模拟器SearchableDictionary样的工作,我发现的第一个搜索调用工作正常,但随之而来的呼叫建立两个搜索事件,所以他们处理两次。有人提到在钛的错误。 (<一href="http://developer.appcelerator.com/question/127166/android-search-keyboardtype-fires-return-event-twice">http://developer.appcelerator.com/question/127166/android-search-keyboardtype-fires-return-event-twice )

我测试过一个真正的三星平板的应用程序,我没有看到两个搜索事件,所以我想这是一个模拟器的问题,而不是一个code问题。

I'm trying to create my first Android app and in the process of adding to it SEARCH functionality. I've followed the Android Developer documentation to add both the Search dialog and widget. Unfortunately, whenever I perform a search, the "onCreate" AND the "onNewIntent" of the search Activity are called. That is, by typing something in the Action Bar Search box and hitting ENTER, the search is called TWICE. I'm stuck. Is some global flag supposed to be returned from the Searchable activity notifying the app that the search has been completed? Are BOTH the Search dialog AND widget being called?

I've searched previous posts on this site and on the web to no avail. Thank you for any help.

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.shop"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <service android:name="com.shop.RestIntentService" />

        <activity
            android:name="com.shop.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name="com.shop.CatalogActivity"
            android:label="@string/app_name" >
        </activity>

        <activity
            android:name="com.shop.SearchableActivity"
            android:launchMode="singleTop" >
            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>

            <!--
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
            </intent-filter>
            -->

            <meta-data
                android:name="android.app.searchable"
                android:resource="@xml/searchable" />
        </activity>

        <meta-data
            android:name="android.app.default_searchable"
            android:value="com.shop.SearchableActivity" />

    </application>

</manifest>

SearchableActivity.java

 public class SearchableActivity extends ListActivity implements Receiver {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        System.out.println("onCreate");
        handleIntent(getIntent());
    }

    @Override
    public void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        System.out.println("onNewIntent");
        setIntent(intent);
        handleIntent(intent);
    }

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

    private void doSearch(String queryStr) {
        System.out.println("searching..." + queryStr);
    }

CatalogActivity.java

@Override   
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.sample, menu);

        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);

        SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();

        searchView.setSearchableInfo(searchManager
                .getSearchableInfo(getComponentName()));

        //return super.onCreateOptionsMenu(menu);
        return true;
    }   

sample.xml

<item
    android:id="@+id/menu_search"
    android:actionViewClass="android.widget.SearchView"
    android:icon="@android:drawable/ic_menu_search"
    android:showAsAction="ifRoom|collapseActionView"
    android:title="@string/menu_search"/>

解决方案

I think methods onNewIntent and onCreate are mutually exclusive. You set launchMode to "singleTop", so the method onNewIntent would be called and not the onCreate.

I have a similar problem when one search request is processed twice. Working with the SearchableDictionary sample in the WXGA 10.1 tablet emulator I found that the first search call works fine, but consequent calls create two SEARCH events, so they are processed twice. Somebody mentioned about a bug in Ti. (http://developer.appcelerator.com/question/127166/android-search-keyboardtype-fires-return-event-twice )

I tested apps on a real Samsung tablet and I didn’t see two SEARCH events, so I guess it’s an emulator problem, not a code problem.

这篇关于检索的活动被称为两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆