简体   繁体   中英

Search interface starting new activities on search android

I am learning to work with the search bar. After the user hits search, it starts 2 new activities and these new activities no longer have a search bar. It also does not update the UI. I am looking to do a search and post the results all in the same activity, if possible. I am using Genymotion emmulator.

Main Activity

public class MainActivity extends Activity {

protected LibraryDataSource mDataSource;
protected ArrayList<String> mBookTitles;
protected ArrayList<Integer> mBookPages;

private EditText mEditText;

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

    mDataSource = new LibraryDataSource(MainActivity.this);
    mBookTitles = new ArrayList<>();
    mBookPages = new ArrayList<>();

    Book book1 = new Book("Harry potter", 15);
    Book book2 = new Book("Far Cry", 25);
    Book book3 = new Book("Jumpman", 35);

    List<Book> books = new ArrayList<>();
    books.add(book1);
    books.add(book2);
    books.add(book3);

    mDataSource.open();
    mDataSource.deleteAll();
    mDataSource.insertBook(books);

    mEditText = (EditText) findViewById(R.id.title_type);

}

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

    // Associate searchable configuration with the SearchView
    SearchManager searchManager =
        (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView =
        (SearchView) menu.findItem(R.id.search).getActionView();
    searchView.setSearchableInfo(
        searchManager.getSearchableInfo(MainActivity.this.getComponentName()));

    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.search) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

Android Mainfest

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
    <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            >
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>

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

        <meta-data
                android:name="android.app.default_searchable"
                android:value=".SearchBookActivity" />

    </activity>

    <activity android:name=".SearchBookActivity">
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />

        </intent-filter>

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

</application>

SearchBookActivity

public class SearchBookActivity extends Activity {

LibraryDataSource mDataSource;
private EditText mEditText;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mEditText = (EditText) findViewById(R.id.title_type);
    mDataSource = new LibraryDataSource(SearchBookActivity.this);
    handleIntent(getIntent());
}

@Override
protected void onNewIntent(Intent intent) {

    setIntent(intent);
    handleIntent(intent);
}

private void handleIntent(Intent intent) {

    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        String query = intent.getStringExtra(SearchManager.QUERY);
        //use the query to search your data somehow

        Log.i("Search bar", query);
        mDataSource.open();
        mEditText.setText(mDataSource.searchKeyString(query));

    }
}
}

I would use both <meta-data> tags together in the same activity where I want to centralise the Search feature. Although you could have more than one activity that supports search. Material Design apps show that having only one Search UI, Fragment or an Activity, is more effective and best practices for app production.

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

The daily case is searching a collection of data in a collection of data. Grids, Lists, etc.

You could have only one search Activity in your app that would get called each time you an Intent with action Search is started. This means from your app and from others. That's should for instance add permissions to your activity making sure that is not available to other apps unless is its function.

<meta-data
    android:name="android.app.default_searchable"
    android:value=".ui.activity.SearchBookActivity" />

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

The above code will bring the SearchBookActivity when an Intent with android.intent.action.SEARCH is started. Then you should already be showing the Search View layout which will get the focus.

Another implementation would be to use a Fragment which doesn't require to listen to SEARCH actions or define any default search activity or permission.

Many apps do not require such a feature and use ListFragments with support of searching in ListView or RecyclerView s.

Either way, whether you use an Activity or a Fragment you would need to inflate the menu:

//From a Fragment

@Override
public void onCreateOptionsMenu(final Menu menu, MenuInflater inflater )
{
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.menu_search, menu);
}

//From an Activity use public boolean onCreateOptionsMenu(Menu menu) in an Actiivty

with menu_search.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item android:id="@+id/search"
          android:title="@string/search_label"
          android:icon="@drawable/ic_actionbar_search"
          app:actionViewClass="android.widget.SearchView"
          android:actionViewClass="android.widget.SearchView"
          app:showAsAction="always"/>
</menu>

Finally but not least. You could remove this code from the MainActivity. In the main activity you just need a menu item with a search icon.

// Associate searchable configuration with the SearchView
SearchManager searchManager =
     (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
     (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(
    searchManager.getSearchableInfo(MainActivity.this.getComponentName()));

Hope you implement your Search!! Check out Material Design.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM