方向改变时片段加载多次

Sha*_*awn 1 android screen-orientation android-fragments

当我更改模拟器或手机上的方向时,片段会额外加载一次。如果我将设备旋转 3 次,则片段将在最后一次旋转后加载 3 次;如果我将设备旋转 5 次,则片段将在最后一次旋转后加载 5 次。

主要活动

...
FragmentOne fragment = new FragmentOne();

FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.main_container, fragmnet);
transaction.commit();
Run Code Online (Sandbox Code Playgroud)

我在片段中唯一拥有的就是日志,没有任何东西会导致片段多次加载。片段一

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        Log.d(TAG, "onCreateView: ");
        return inflater.inflate(R.layout.fragment_fragment_one, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Log.d(TAG, "onViewCreated: ");
    }
Run Code Online (Sandbox Code Playgroud)

这是一次旋转后的日志副本,请注意 onCreateView 在一次方向更改时被多次调用。

2020-04-13 01:23:07.396 10857-10857/com.example.myapplication D/MYFragmentOne: onCreate: 
2020-04-13 01:23:07.396 10857-10857/com.example.myapplication D/MYFragmentOne: onCreate: 
2020-04-13 01:23:07.418 10857-10857/com.example.myapplication D/MYMainActivity: onCreate: 
2020-04-13 01:23:07.423 10857-10857/com.example.myapplication D/MYFragmentOne: onCreateView: 
2020-04-13 01:23:07.429 10857-10857/com.example.myapplication D/MYFragmentOne: onViewCreated: 
2020-04-13 01:23:07.429 10857-10857/com.example.myapplication D/MYFragmentOne: onCreateView: 
2020-04-13 01:23:07.431 10857-10857/com.example.myapplication D/MYFragmentOne: onViewCreated: 
2020-04-13 01:23:07.431 10857-10857/com.example.myapplication D/MYFragmentOne: onCreate: 
2020-04-13 01:23:07.431 10857-10857/com.example.myapplication D/MYFragmentOne: onCreateView: 
2020-04-13 01:23:07.433 10857-10857/com.example.myapplication D/MYFragmentOne: onViewCreated:



App details
compileSdkVersion 29
buildToolsVersion "29.0.2"
applicationId "com.example.myapplication"
minSdkVersion 29
targetSdkVersion 29
Run Code Online (Sandbox Code Playgroud)

是什么会导致片段在方向变化的情况下加载这么多次?

这是我的github链接的链接

Ang*_*i H 7

当 Activity 方向发生变化时,它将onCreate()再次调用,并且您放入其中的片段也将被重新创建。在这种情况下,您应该仅在活动第一次加载时创建片段。然后在下次加载时获取保存的片段实例。用作tag钥匙Fragment

private static final String FRAGMENT_TAG = "fragmentOne";

private Fragment fragment;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_settings)

    // savedInstanceState always null on activity's first load
    if (savedInstanceState == null) {
        fragment = new FragmentOne();
    } else {
        fragment = getSupportFragmentManager().findFragmentByTag(FRAGMENT_TAG);
    }

    getSupportFragmentManager().beginTransaction()
        .replace(R.id.main_container, fragment, FRAGMENT_TAG)
        .commit();
}
Run Code Online (Sandbox Code Playgroud)