vendredi 22 mai 2015

Android: Intent.getAction() returns null on search bar query

I have a search view in action bar on my activity. I could show the search icon on the action bar on my UI as per android documentation mentioned here

http://ift.tt/1iCOrHC

However, when I type the query in search view, the query does not passes to my fragment. On more debugging I found that following code intent.getAction() is returning null

Intent intent = getIntent();
Log.d("get action: ", String.valueOf(intent.getAction()));

I tried following and related solutions suggested on stack overflow, but none of them worked for me.

Cannot get searchview in actionbar to work

Following is my code. I am pretty sure there is some problem in my android manifest file, but i need a fresh set of eyes to help me out with this problem. Thanks a lot in advance !

Following is the code.

Android-Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://ift.tt/nIICcg"
        package="esp" >

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

        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme"
            android:name="esp.Controller.AppCtrl">
            <activity
                android:name=".View.LoginActivity"
                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="esp.view.CpitActivity"
                android:label="@string/title_activity_cpit"
                android:parentActivityName=".View.LoginActivity"
                android:theme="@style/CustomActionBarTheme" >
                <meta-data
                    android:name="android.support.PARENT_ACTIVITY"
                    android:value="esp.View.LoginActivity" />
            </activity>
            <activity
                android:name="esp.orgs.View.OrgActView"
                android:label="@string/title_orgs_activity"
                android:parentActivityName="esp.view.CpitActivity"
                android:theme="@style/CustomActionBarTheme" >
                <intent-filter>
                    <action android:name="android.intent.action.SEARCH"/>
                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>

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

    </manifest>

searchable.xml

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://ift.tt/nIICcg"
            android:label="@string/app_label"
            android:hint="@string/search_hint"
            >
</searchable>

In OrgActView.java (this is my activity)

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


        // Get the intent, verify the action and get the query

        Intent intent = getIntent();
        Log.d("get action: ", String.valueOf(intent.getAction()));

        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            String query = intent.getStringExtra(SearchManager.QUERY);
            if (savedInstanceState == null) {
                FragmentManager fragmentManager = getSupportFragmentManager();
                FragmentTransaction transaction = fragmentManager.beginTransaction();
                transaction.addToBackStack("list_orgs");
                transaction.add(R.id.orgs_container, OrgsListView.newInstance(query))
                        .commit();

            }

        }



        m_progressBar = (ProgressBar)findViewById(R.id.pbHeaderProgress);
        m_state = "HIDE_EDIT";
        supportInvalidateOptionsMenu();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.menu_menu_bar, menu);
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
        ComponentName compName = new ComponentName(this, OrgActView.class);
        if(searchManager.getSearchableInfo(compName) == null)
            Log.d("its null","->>");
        else
            Log.d("its not null","->>");

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



        return true;
    }

Aucun commentaire:

Enregistrer un commentaire