我使用搜索小部件创建了一个搜索视图,当我打开并搜索它正常工作时,问题是我去任何活动,包括searchview活动,然后回到使用backbutton或finish()活动,搜索小部件停止工作。我的意思是,我仍然可以键入小部件,但搜索按钮(返回)停止工作,可搜索的活动不再打开。
在HomeActivity.java(main)中,我设置了搜索小部件:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_home_settings, menu);
// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
// Assumes current activity is the searchable activity
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default
// Change Text color from search bar
final EditText searchEditText = (EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
searchEditText.setImeOptions(DEFAULT_KEYS_SEARCH_LOCAL);
return true;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_searchable);
Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
if(getSupportActionBar() != null && toolbar != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toolbar.setTitle("");
}
.
.
.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_search, menu);
// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
// Assumes current activity is the searchable activity
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
final EditText searchEditText = (EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
searchEditText.setImeOptions(DEFAULT_KEYS_SEARCH_LOCAL);
return true;
}
@Override
protected void onNewIntent(Intent intent) {
setIntent( intent );
handleSearch( getIntent() );
}
public void handleSearch(Intent intent){
if( Intent.ACTION_SEARCH.equalsIgnoreCase( intent.getAction() )){
String query = intent.getStringExtra(SearchManager.QUERY);
setTitle(query);
executeSearchShow(query);
}
}
正如我所说,使用backButton返回或调用活动上的finish(),searchview小部件停止工作,但是如果我使用homeUpButton它会正常工作。
我在Searchable活动中覆盖onBackPressed并且它可以工作,但是如果我从另一个活动(我需要完成)回到主页,那么小部件仍然停止工作。
@Override
public void onBackPressed() {
onNavigateUp();
}
AndroidManifest.xml:
<activity
android:name=".activities.HomeActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/app_name"
android:theme="@style/Theme.MySeries"
android:windowSoftInputMode="adjustPan|adjustNothing">
<meta-data
android:name="android.app.default_searchable"
android:value="br.com.adley.whatsnextseries.activities.Searchable"/>
</activity>
<activity android:name=".activities.Searchable"
android:launchMode="singleTop" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable"/>
</activity>