Results 1 to 12 of 12
Like Tree1Likes
  • 1 Post By Tolls

Thread: Pressing Search button does not fire up the onCreate or onNewIntent methods

  1. #1
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    7

    Default Pressing Search button does not fire up the onCreate or onNewIntent methods

    I wrote this example app to test the Android Search Framework using Search Widget. I have followed this developer guide, but when I run the app, pressing the search button does not execute search. It does nothing.

    From the `Log.i` statements that I added for checking purpose show that onNewIntent is NOT called when the Search button is pressed, although the launchMode is singleTop.

    What am I doing wrong? How can I fix this? If somebody can test it, I'll be thankful.

    Logcat:
    Java Code:
    06-01 12:49:44.900  12168-12168/? I/SearchActivity﹕ onCreate called.
    ...
    06-01 12:49:44.950  12168-12168/? I/SearchActivity﹕ handleIntent called.
    06-01 12:49:44.950  12168-12168/? I/SearchActivity﹕ dumpIntent called.
    06-01 12:49:44.950  12168-12168/? I/SearchActivity﹕ Bundle returned from getExtras() is null.
    ...
    06-01 12:49:45.420  12168-12184/? D/OpenGLRenderer﹕ Enabling debug mode 0
    ...
    06-01 12:49:45.540  12168-12184/? W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0x7f4421caba00, error=EGL_SUCCESS
    SearchActivity.java:
    Java Code:
    package practice_projects.assisted_search_using_search_widget;
    
    import android.app.Activity;
    import android.app.SearchManager;
    import android.app.SearchableInfo;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v7.widget.SearchView;
    import android.util.Log;
    import android.widget.TextView;
    
    import java.util.Set;
    import java.util.Iterator;
    
    
    public class SearchActivity extends Activity {
        TextView textView;
        private static String TAG = SearchActivity.class.getSimpleName();
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            Log.i(TAG, "onCreate called.");
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.activity_search);
            textView = (TextView) findViewById(R.id.searchActivity_textView);
    
            SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
            SearchableInfo searchableInfo = searchManager.getSearchableInfo(getComponentName());
            SearchView searchView = (SearchView) findViewById(R.id.searchActivity_searchView);
            searchView.setSearchableInfo(searchableInfo);
    
            handleIntent(getIntent());
        }
    
    
        @Override
        protected void onNewIntent(Intent intent) {
            Log.i(TAG, "onNewIntent called. ");
            setIntent(intent);
            handleIntent(intent);
        }
    
    
        /**
         *
         */
        private void handleIntent(Intent intent) {
            Log.i(TAG, "handleIntent called.");
            dumpIntent(intent);
            if (intent.getAction().equals(Intent.ACTION_SEARCH)) {
                Log.i(TAG, "Search query delivered in handleIntent.");
                performSearch(intent.getStringExtra(SearchManager.QUERY));
            }
        }
    
    
        /**
         *
         */
        private void performSearch(String searchQuery) {
            Log.i(TAG, "performSearch called.");
            textView.setText(searchQuery);
        }
    
    
        /**
         * Utility method to print intent's extras' keys and values. From http://stackoverflow.com/questions/5968896/listing-all-extras-of-an-intent
         */
        public static void dumpIntent(Intent i){
            Log.i(TAG, "dumpIntent called.");
            Bundle bundle = i.getExtras();
            if (bundle != null) {
                Set<String> keys = bundle.keySet();
                Iterator<String> it = keys.iterator();
                Log.i(TAG,"Dumping Intent start");
                while (it.hasNext()) {
                    String key = it.next();
                    Log.i(TAG,"[" + key + "=" + bundle.get(key)+"]");
                }
                Log.i(TAG,"Dumping Intent end");
            } else {
                Log.i(TAG, "Bundle returned from getExtras() is null.");
            }
        }
    
    }
    activity_search:
    Java Code:
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
    
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
    
        tools:context="${relativePackage}.${activityClass}" >
    
        <android.support.v7.widget.SearchView
            android:id="@+id/searchActivity_searchView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
        <TextView
            android:id="@+id/searchActivity_textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:color="#E1F5A9" />
    
    </LinearLayout>
    Manifest:
    Java Code:
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="practice_projects.assisted_search_using_search_widget" >
    
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="22" />
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
    
            <activity
                android:name=".SearchActivity"
                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>
    
            <meta-data android:name="android.app.default_searchable"
                android:value=".SearchActivity" />
        </application>
    
    </manifest>
    res/xml/searchable.xml:
    Java Code:
    <?xml version="1.0" encoding="utf-8"?>
    <searchable xmlns:android="http://schemas.android.com/apk/res/android"
        android:name="@string/app_name"
        android:hint="@string/searchView_hint" >
    </searchable>
    res/values/strings.xml:
    Java Code:
    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    
        <string name="app_name">Assisted Search Using Search Widget</string>
        <string name="searchView_hint">Search Zarah</string>
    
    </resources>
    Note: I had also posted it here.
    Last edited by Zarah; 06-01-2015 at 09:55 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Pressing Search button does not fire up the onCreate or onNewIntent methods

    A suggestion: Add some more info to the Log messages. For example the value of the Intent passed to handleIntent().
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    7

    Default Re: Pressing Search button does not fire up the onCreate or onNewIntent methods

    A suggestion: Add some more info to the Log messages. For example the value of the Intent passed to handleIntent().
    Thank you for the suggestion. But the problem is that at first when the activity is launched, onCreate is called and from the body of onCreate, handleIntent is called. So we get the corresponding `Log.i` messages in the Logcat. But as the user types a search query in the SearchView and presses the Search button, nothing happens. The system doesn't invoke anything..

    But I tested it anyway and updated the code and Logcat in the question, and added a method to log the keys and values from all the extras carried by the intent delivered to handleInten, and called it from handleIntent. The result: There are no extras, as the Bundle returned by calling getExtras on the intent is Null.

    I am guessing that the Android search framework is just not getting activated, so there is probably something wrong in one or more of three things:

    1. Something with res/xml/searchable.xml file. I did recheck its location, and the two elements it contains. Everything seems correct.
    2. Something in the Manifest in the <activity> element or <meta-data> element in <application>
    3. In location or code of the following snippet:

    Java Code:
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
            SearchableInfo searchableInfo = searchManager.getSearchableInfo(getComponentName());
            SearchView searchView = (SearchView) findViewById(R.id.searchActivity_searchView);
            searchView.setSearchableInfo(searchableInfo);
    But if there is a fault, my eyes are missing it.
    Last edited by Zarah; 06-01-2015 at 10:13 AM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Pressing Search button does not fire up the onCreate or onNewIntent methods

    Can you make a minimal project that shows the problem, zip the whole thing and attach it here?
    It's hard to debug Android projects without (almost) working code.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    7

    Default Re: Pressing Search button does not fire up the onCreate or onNewIntent methods

    Can you make a minimal project that shows the problem, zip the whole thing and attach it here?
    I am attaching the Android Studio project project in .zip format.
    Attached Files Attached Files

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Pressing Search button does not fire up the onCreate or onNewIntent methods

    Have you read this site and followed its directions?
    Creating a Search Interface | Android Developers
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    7

    Default Re: Pressing Search button does not fire up the onCreate or onNewIntent methods

    Have you read this site and followed its directions?
    Creating a Search Interface | Android Developers
    Yes, this is the tutorial I had used to write this application.

    EDIT: I am sorry, I just saw that I had used `android:name` in place of `android:label` in the `searchable.xml` file. I had read that developer guide and then skimmed through it so many times but somehow missed it. I need to be more alert when I read. I have learnt that I will read the sample code snippets very carefully when I read the developer guides again to find out my mistakes.
    Last edited by Zarah; 06-04-2015 at 12:08 AM.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Pressing Search button does not fire up the onCreate or onNewIntent methods

    Check that you have followed it. I don't use Android Studio and had to convert it to Eclipse. I made some changes as suggested in that tutorial to get it to work.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    7

    Default Re: Pressing Search button does not fire up the onCreate or onNewIntent methods

    Check that you have followed it. I don't use Android Studio and had to convert it to Eclipse. I made some changes as suggested in that tutorial to get it to work.
    I am sorry, somehow I was not alert enough when writing the code and then cross-checking it for mistakes. I had used `android:name` in place of `android:label` in `searchable.xml`.

    About Android Studio, I had this project in Eclipse as well, and I thought about uploading it many times, but I have been suggested to switch to Android Studio countless times, so I thought everyone else uses Android Studio. In future if I upload any project, I'll upload both the versions.

    Thank you very much for your time.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Pressing Search button does not fire up the onCreate or onNewIntent methods

    Did that change fix your problem?
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    7

    Default Re: Pressing Search button does not fire up the onCreate or onNewIntent methods

    Quote Originally Posted by Norm View Post
    Did that change fix your problem?
    Yes, it did.

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Pressing Search button does not fire up the onCreate or onNewIntent methods

    Quote Originally Posted by Zarah View Post
    I am sorry, somehow I was not alert enough when writing the code and then cross-checking it for mistakes. I had used `android:name` in place of `android:label` in `searchable.xml`.
    Happens all the time, no matter how long you've been doing this.
    Staring at code eventually results in code blindness, at which point you just need to walk away for a bit...or get someone else to look at the code.
    :)
    Zarah likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 5
    Last Post: 12-03-2012, 07:01 AM
  2. pressing a button making a instance in another class
    By SpicyElectricity in forum New To Java
    Replies: 3
    Last Post: 02-18-2012, 10:48 PM
  3. Draw shapes by pressing a button
    By SWEngineer in forum AWT / Swing
    Replies: 16
    Last Post: 05-14-2010, 05:26 AM
  4. Replies: 2
    Last Post: 04-12-2009, 10:39 AM
  5. Replies: 5
    Last Post: 01-17-2008, 09:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •