gpt4 book ai didi

android - MainActivity 在 QueryTextSubmit 上重新启动

转载 作者:太空狗 更新时间:2023-10-29 14:16:20 25 4
gpt4 key购买 nike

我已经设置了可搜索配置,但现在我遇到了一个奇怪的问题。当我提交搜索查询时,SearchActivity.class 会启动一秒钟,然后返回到 MainActivity.class。我不知道,会发生什么。

查看我的代码:

MainActivity中:

@Override
public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);

MenuItem searchItem = menu.findItem(R.id.action_search);
mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);

if (null != mSearchView )
{
mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

}

SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener()
{
public boolean onQueryTextChange(String newText)
{


return true;
}

public boolean onQueryTextSubmit(String query)
{
String said = query.toString();
Log.v(said, "submited");
Intent intent = new Intent(getApplicationContext(), SearchActivity.class);
intent.setAction(Intent.ACTION_SEARCH);
intent.putExtra("query", query);
startActivity(intent);

return true;

}
};
mSearchView.setOnQueryTextListener(queryTextListener);

return super.onCreateOptionsMenu(menu);
}

AndroidManifest:

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.animalist.MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait" >
<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>

<activity
android:name="com.example.animalist.MoreActivity"
android:label="@string/title_activity_more"
android:launchMode="singleTop"
android:parentActivityName="com.example.animalist.MainActivity"
android:screenOrientation="portrait" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.animalist.MainActivity" />
</activity>

<activity
android:name="com.example.animalist.SearchActivity"
android:label="@string/title_activity_search"
android:launchMode="singleTop"
android:parentActivityName="com.example.animalist.MainActivity"
android:screenOrientation="portrait" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.animalist.MainActivity" />

</activity>

</application>

我做错了什么?

最佳答案

基于 this thread , 看来是Android的bug。

here 找到这个临时“解决方案” (日本人)。它对我有用。

@Override
public boolean onQueryTextSubmit(String query) {
mSearchView.clearFocus();

......
// your code to process the query
......

return false;
}

关于android - MainActivity 在 QueryTextSubmit 上重新启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21660951/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com