I load a fragment, in an Activity that the activity will be re-established when I rotate the screen, but shouldn"t the fragment in it be destroyed only once with activity? Why was it destroyed twice?
and why are people talking about screen rotation fragment overlap? When the activity is destroyed, isn"t the fragment associated with it destroyed as well? When you create a new activity, there is only one fragment,. How can it overlap?
the rookie said he was confused. I hope God can solve my question. thank you!
MainActivity
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager manager=getSupportFragmentManager();
FragmentTransaction transaction=manager.beginTransaction();
FragmentOne fragmentOne=new FragmentOne();
transaction.replace(R.id.fragment_container,fragmentOne,"one");
transaction.commit();
}
FragmentOne
public class FragmentOne extends Fragment {
private final String TAG=FragmentOne.class.getSimpleName();
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG,"onCreate");
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.fragment_one,container,false);
Button button=v.findViewById(R.id.create_fragment);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager manager=getFragmentManager();
FragmentTransaction transaction=manager.beginTransaction();
transaction.replace(R.id.fragment_container,new FragmentTwo(),"two");
// transaction.addToBackStack("one");
transaction.commit();
}
});
Log.i(TAG,"onCreateView");
return v;
}
@Override
public void onDestroyView() {
super.onDestroyView();
Log.i(TAG,"onDestroyView");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i(TAG,"onDestroy");
}
@Override
public void onDetach() {
super.onDetach();
Log.i(TAG,"onDetach");
}
}