在 onActivityCreated() 之后再次调用 Fragment 的 onAttach()
Posted
技术标签:
【中文标题】在 onActivityCreated() 之后再次调用 Fragment 的 onAttach()【英文标题】:Fragment's onAttach() called again after onActivityCreated() 【发布时间】:2019-10-25 01:52:06 【问题描述】:我正在添加一个带有编辑文本和复选框的片段,并且我正在尝试在方向更改后保存视图的状态,但我无法这样做。 谁能告诉我为什么在 onActivityCreated() 之后再次调用 onAttach() 方法?当我旋转我的设备一次时会发生这种情况,因此我无法保存我的状态。这真的很奇怪,我被困在这个问题上。
BlankFragment.java
@Override
public void onAttach(Context context)
super.onAttach(context);
Log.d("loggggggggg","onAttach() called!");
@Override
public void onCreate(@Nullable Bundle savedInstanceState)
super.onCreate(savedInstanceState);
Log.d("loggggggggg","onCreate() called!");
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
Log.d("loggggggggg","onCreateView() called!");
View v = inflater.inflate(R.layout.fragment_blank, container, false);
ed = v.findViewById(R.id.edit);
cb = v.findViewById(R.id.check);
if(savedInstanceState != null)
ed.setText(savedInstanceState.getString("string"));
cb.setChecked(savedInstanceState.getBoolean("bool"));
return v;
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState)
super.onActivityCreated(savedInstanceState);
Log.d("loggggggggg","onActivityCreated() called!");
@Override
public void onStart()
super.onStart();
Log.d("loggggggggg","onStart() called!");
@Override
public void onResume()
super.onResume();
Log.d("loggggggggg","onResume() called!");
@Override
public void onPause()
super.onPause();
Log.d("loggggggggg","onPause() called!");
@Override
public void onStop()
super.onStop();
Log.d("loggggggggg","onStop() called!");
@Override
public void onDestroyView()
super.onDestroyView();
Log.d("loggggggggg","onDestroyView() called!");
@Override
public void onDestroy()
super.onDestroy();
Log.d("loggggggggg","onDestroy() called!");
@Override
public void onDetach()
super.onDetach();
Log.d("loggggggggg","onDetach() called!");
@Override
public void onSaveInstanceState(@NonNull Bundle outState)
Log.d("loggggggggg","onSavedInstance() called!");
outState.putString("string",ed.getText().toString());
outState.putBoolean("bool",cb.isChecked());
super.onSaveInstanceState(outState);
Log after screen rotation
D/loggggggggg: onPause() called!
D/loggggggggg: onSavedInstance() called!
D/loggggggggg: onStop() called!
D/loggggggggg: onDestroyView() called!
D/loggggggggg: onDestroy() called!
onDetach() called!
D/loggggggggg: onAttach() called!
onCreate() called!
D/loggggggggg: onCreateView() called!
D/loggggggggg: onActivityCreated() called!
D/loggggggggg: onAttach() called! //why is this happening
onCreate() called!
D/loggggggggg: onDestroyView() called!
onDestroy() called!
onDetach() called!
D/loggggggggg: onCreateView() called!
D/loggggggggg: onActivityCreated() called!
onStart() called!
D/loggggggggg: onResume() called!
【问题讨论】:
也请添加日志 我在最后的代码后面加了 我的意思是显示来自 logcat 的日志 【参考方案1】:您可以删除 onAttach() 方法,因为它是不必要的 :) 你可以告诉我们 logcat 的错误是什么:)
【讨论】:
以上是关于在 onActivityCreated() 之后再次调用 Fragment 的 onAttach()的主要内容,如果未能解决你的问题,请参考以下文章
onActivityCreated() 中的捆绑返回 null,即使在 onSaveInstanceState() 中设置值之后也是如此
Android 片段 onCreateView 与 onActivityCreated
如何在 OnActivityCreated 中解决这个不受欢迎的问题?
onActivityCreated 已弃用,如何正确使用 LifecycleObserver?
为啥我们可以在 onActivityCreated 之前运行的 onCreateView 中调用 getActivity()?
在 onActivityCreated/onStart/onViewCreated 方法内的 getView() 上出现 NullPointerException 警告