机器人onCreateOptionsMenu叫了两声时恢复状态 [英] android onCreateOptionsMenu called twice when restoring state
问题描述
下面是我自己制作的展示我的问题一个简单的Android应用程序:
Here's a simple android app I've created to demonstrate my problem:
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倾倒片段为:
Layout is just a LinearLayout to dump the fragment into:
<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>
很简单吧?当我运行它,我得到预期的输出如下:
Very simple right? When I run it I get the following output as expected:
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
现在,当我旋转手机,我得到一些奇怪的行为:
Now when I rotate the phone I get some strange behavior:
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叫了两声?如果我删除的片段选项菜单中,我得到1调用活动onCreateOptionsMenu预期:
Why is the activity onCreateOptionMenu and fragment onCreateOptionsMenu called twice? If I remove the options menu from the fragment I get 1 call to the activity onCreateOptionsMenu as expected:
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
我不明白这一点,并没有其他人似乎也遇到了这个问题。真正的问题是,我的搜索查看是不是能够恢复它的状态上的配置变化(手机旋转),因为onCreateOptionMenu被调用两次。第一次它似乎有它的状态,而是第二次它清除出去,并复位。我不能找出我做错了。
I don't understand this and no one else seems to have encountered this problem. The real problem is that my SearchView is not able to recover it's state on config change (phone rotate) because the onCreateOptionMenu is being called twice. The first time it seems to have it's state but the second time it's cleared out and reset. I'm not able to figure out what I'm doing wrong.
在此先感谢。
推荐答案
我想我找到了答案,这个问题。
I think I found the answer to this problem.
看看一个这样的:
http://stackoverflow.com/a/7225296/48468
的问题似乎与这样的事实,机器人不破坏片段时的活性被破坏(当设备旋转)。
The problem seems to be related to the fact that Android does not destroy the fragment when the activity is destroyed (when the device is rotated).
基本上我说:
setRetainInstance(true);
我的片段构造和问题去解决。
to my fragment constructor and the problem go solved.
希望它能帮助!
这篇关于机器人onCreateOptionsMenu叫了两声时恢复状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!