- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个应在纵向模式下仅显示 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/
我正在开发一个使用多个 turtle 的滚动游戏。玩家 turtle 根据按键命令在 Y 轴上移动。当危害和好处在 X 轴上移动时,然后循环并改变 Y 轴位置。我尝试定义一个名为 colliding(
我不明白为什么他们不接受这个作为解决方案,他们说这是一个错误的答案:- #include int main(void) { int val=0; printf("Input:- \n
我正在使用基于表单的身份验证。 我有一个注销链接,如下所示: 以及对应的注销方法: public String logout() { FacesContext.getCurren
在 IIS7 应用程序池中有一个设置 Idle-time out 默认是 20 分钟,其中说: Amount of time(in minutes) a worker process will rem
我是一名优秀的程序员,十分优秀!