简体   繁体   中英

Fragment's onAttach() called again after onActivityCreated()

I am adding a fragment with an edittext and a checkbox and I am trying to save view's state after an orientation change but I am not able to do so. Can anybody tell me why the method onAttach() called again after onActivityCreated()? This happens when I rotate my device once and due to this I am not able to save my state. It is really weird and I am stuck on this badly.

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!

你可以删除方法onAttach()'因为它的无用:)你可以告诉我们logcat的错误是什么:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM