我在主要活动中实现了我的搜索小部件..而且我的MainActivity正在处理所有搜索功能.. 执行查询时,结果按照需要出现。 但真正的问题是,如果我想回到基本的MainActivity内容,搜索查询结果是可见的,那么我尝试按Home / Up按钮或设备返回按钮... 按Home / Up按钮没有任何反应..只需打开搜索小部件崩溃... 但按下后退按钮,应用程序退出..
我想要的是..我想在显示搜索内容后返回MainActivity的基本开始结果而不退出应用程序..
这是我的Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.pockettutsforcosm"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="20" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:launchMode="singleTop"
android:parentActivityName=".MainActivity" >
<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=".MainActivity" />
</application>
</manifest>
更新 MainActivity Source -
@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
// Only show items in the action bar relevant to this screen
// if the drawer is not showing. Otherwise, let the drawer
// decide what to show in the action bar.
getMenuInflater().inflate(R.menu.main, menu);
// Associate searchable configuration with the SearchView
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()));
// Do not iconify the widget; expand it by default
searchView.setIconifiedByDefault(false);
searchView.setSubmitButtonEnabled(true);
restoreActionBar();
return true;
}
return super.onCreateOptionsMenu(menu);}
我正在使用以下代码处理它 -
@Override
protected void onNewIntent(Intent intent)
{
setIntent(intent);
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String searchText = intent.getStringExtra(SearchManager.QUERY);
doMySearch(searchText);
// Do work using string
}
}
private void doMySearch(String searchText)
{
c = dbOpen.getMySearchFromDB(searchText);
showListContent(c);
}
我打电话给 -
handleIntent(getIntent);
在MainActivity的onCreate()