I have an Activity where I used a ListView to display some data and I also want to do some filtering by using SearchView and I want to display the filtered results in the same Activity.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setSubmitButtonEnabled(true);
return true;
}
I am doing the search correctly but I noticed that when ever I make a new search process a new Activity instance gets created. So, I wanted to launch the Activity as singleTop by putting android:launchMode="singleTop" to my manifest and tried to handle the search process in onNewIntent method. That did not work as well. The Activity still gets created anytime I make a new search.
How can I avoid this ? Any help would be appreciated.
I'll try to give an example.
Suppose we have a layout file activity_main.xml
menu_main.xml
And here's the MainActivity.java file
When you search any element, filtered results should be displayed in same activity. Hope it helps!
styles.xml file