gpt4 book ai didi

Android Fragment 生命周期方向改变

转载 作者:行者123 更新时间:2023-11-29 02:19:15 25 4
gpt4 key购买 nike

我有一个应在纵向模式下显示 fragment 的 Activity 。所以,我的纵向布局有容器 View ,而我的横向布局没有。然而,在改变方向后,肖像->风景 fragment 被重新创建,即使它没有容器可以显示。
此外,这个重新创建的 Fragment 是真正活跃的并且响应所有生命周期事件,例如 onPause、onResume 等。

日志:

A{6bee548 #2 id=0x7f07002c tag} - ON_CREATE
A{6bee548 #2 id=0x7f07002c tag} - ON_START
A{6bee548 #2 id=0x7f07002c tag} - ON_RESUME
*** orientation change ***
A{6bee548 #2 id=0x7f07002c tag} - ON_PAUSE
A{6bee548 #2 id=0x7f07002c tag} - ON_STOP
A{6bee548 #2 id=0x7f07002c tag} - ON_DESTROY
Activity: Nowhere to attach!
A{bf65baf #2 id=0x7f07002c tag} - ON_CREATE
A{bf65baf #2 id=0x7f07002c tag} - ON_START
A{bf65baf #2 id=0x7f07002c tag} - ON_RESUME

如何避免在方向改变后创建 fragment ?

这是我所有的:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

View container = findViewById(R.id.container);
if (container != null) {
getSupportFragmentManager().beginTransaction()
.replace(container.getId(), new A(), "tag")
.commit();
} else {
Log.d(MainActivity.class.getSimpleName(), "Nowhere to attach!");
}
}

public static class A extends Fragment {

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment, container, false);
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

getViewLifecycleOwner().getLifecycle().addObserver(new LifecycleEventObserver() {
@Override
public void onStateChanged(@NonNull LifecycleOwner source, @NonNull Lifecycle.Event event) {
Log.d(A.class.getSimpleName(), String.format("%s - %s", A.this.toString(), event.name()));
}
});
}
}
}

布局.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">


<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

layout-land.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Nothing here"/>

</androidx.constraintlayout.widget.ConstraintLayout>

fragment .xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:text="Fragment" />

最佳答案

我目前的解决方案是在 Activity 的 onCreate(...) 方法中传递 null

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
}

但这看起来有风险,因为我可能不是唯一使用保存的实例状态来实际存储状态的人。

关于Android Fragment 生命周期方向改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57509469/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com