在onActivityCreated()之后再次调用片段的onAttach()

时间:2019-06-10 16:30:14

标签: android android-fragments android-activity activity-lifecycle

我正在添加一个带有edittext和一个复选框的片段,并且试图在更改方向后保存视图的状态,但是我无法这样做。 谁能告诉我为什么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!

1 个答案:

答案 0 :(得分:0)

您可以删除onAttach()方法,因为它是不必要的:) 您可以向我们展示logcat的错误是什么:)