这是我创建的一个简单的 android 应用程序来演示我的问题:
public class OptionMenuTest extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("test", "create activity");
setContentView(R.layout.options_layout);
if(getFragmentManager().findFragmentByTag("frag") == null) {
getFragmentManager().beginTransaction().add(R.id.option_fragment_container, new OptionMenuFragment(), "frag").commit();
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
Log.d("test", "saving Activity state");
super.onSaveInstanceState(outState);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
Log.d("test", "create Activity options menu");
menu.add("activity");
return true;
}
}
分段:
public class OptionMenuFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("test", "create fragment");
setHasOptionsMenu(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
TextView tv = new TextView(getActivity());
tv.setText("Hello world");
Log.d("test", "create fragment view");
return tv;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.add("fragment");
Log.d("test", "create fragment options menu");
}
}
布局只是一个 LinearLayout 将片段转储到:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/option_fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</LinearLayout>
很简单吧?当我运行它时,我得到了预期的以下输出:
06-12 15:42:51.415: D/test(957): create activity
06-12 15:42:51.446: D/test(957): create fragment
06-12 15:42:51.446: D/test(957): create fragment view
06-12 15:42:51.446: D/test(957): create Activity options menu
06-12 15:42:51.446: D/test(957): create fragment options menu
现在,当我旋转手机时,我会遇到一些奇怪的行为:
06-12 15:43:11.251: D/test(957): saving Activity state
06-12 15:43:11.290: D/test(957): create fragment
06-12 15:43:11.290: D/test(957): create activity
06-12 15:43:11.306: D/test(957): create fragment view
06-12 15:43:11.306: D/test(957): create Activity options menu
06-12 15:43:11.306: D/test(957): create fragment options menu
06-12 15:43:11.306: D/test(957): create Activity options menu
06-12 15:43:11.306: D/test(957): create fragment options menu
为什么活动 onCreateOptionMenu 和片段 onCreateOptionsMenu 被调用了两次?如果我从片段中删除选项菜单,我会按预期对活动 onCreateOptionsMenu 进行 1 次调用:
06-12 15:50:03.610: D/test(1076): create fragment
06-12 15:50:03.610: D/test(1076): create fragment view
06-12 15:50:03.813: D/test(1076): create Activity options menu
06-12 15:50:08.392: D/test(1076): saving Activity state // <-- rotate happens here
06-12 15:50:08.446: D/test(1076): create fragment
06-12 15:50:08.446: D/test(1076): create activity
06-12 15:50:08.462: D/test(1076): create fragment view
06-12 15:50:08.470: D/test(1076): create Activity options menu
我不明白这一点,似乎没有其他人遇到过这个问题。真正的问题是我的 SearchView 无法在配置更改(手机旋转)时恢复它的状态,因为 onCreateOptionMenu 被调用了两次。第一次它似乎有它的状态,但第二次它被清除并重置。我无法弄清楚我做错了什么。
提前致谢。