【发布时间】:2019-08-15 12:13:43
【问题描述】:
我有一个应该在纵向模式下仅显示片段的活动。所以,我的纵向布局有容器视图和我的横向 - 不要。但是,在更改方向纵向->横向片段后,即使没有要显示的容器,也会重新创建片段。
此外,这个重新创建的 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()));
}
});
}
}
}
layout.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>
片段.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" />
【问题讨论】:
-
你的意思是,当方向改变发生时,你想阻止片段的重新创建?
-
@Kabir ,是的,完全正确。但是添加
setRetainInstance(true)不会有帮助,只会导致 IllegalStateException,因为它会尝试将片段附加到不存在的视图。 -
好的,但是你也可以使用 onSaveInstanceState 来保存片段状态。
-
我不需要保存它 - 我需要忽略它,所以它不会再次创建。
-
那么我在片段的onCreate()中只有两种方法setRetainInstance(true)。你可以保存片段的状态,并在方向发生时取回那个片段。
标签: android android-fragments fragment-lifecycle