我在我的应用程序中使用带有操作栏和 fragment 的搜索 View 。 searchview 在 portrate 模式下打开,当我们改变方向时它会关闭。如果搜索 View 中存在任何文本,它不会在方向更改时关闭。即使搜索 View 为空,我也想在方向更改时保留搜索 View 的状态。我该怎么做?我试过了
setRetainInstance(true);
但它不起作用。请帮助我。
最佳答案
我自己实现了它,因为 Android 似乎没有为我保存它。
final String s = savedSearchString;
if (s != null) {
searchViewMenuItem.expandActionView(); // opens the action view
}
searchView.post(new Runnable() {
@Override
public void run() {
searchView.setQuery(s, false); // sets the last search string on the view
}
});
'savedSearchString' 来自 savedInstanceState。确保将搜索窗口的初始内容设置为 ""而不是 null(默认),否则它不会重新打开空窗口。
关于android - searchview 不再关注方向变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22631005/