被破坏片段的按钮仍然可见
Button of destroyed fragment still visible
我有一个简单的片段:
public class DriverListFragment extends Fragment {
public DriverListFragment() {
}
public static DriverListFragment newInstance(String param1, String param2) {
return new DriverListFragment();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_driver_list, container, false);
}
ListView listViewDriver;
DriverListAdapter adapter;
RoomWithRxJavaViewModel viewModel;
FloatingActionButton btnOpenDriverAddFragment2;
CustomLinearLayout customLinearLayout;
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
listViewDriver = view.findViewById(R.id.listAllDrivers);
viewModel = new RoomWithRxJavaViewModel(getActivity().getApplication());
btnOpenDriverAddFragment2 = view.findViewById(R.id.btnOpenDriverAddFragment2);
customLinearLayout = (CustomLinearLayout) view.findViewById(R.id.cusLL);
Snackbar.make(customLinearLayout, "Text to display", Snackbar.LENGTH_LONG).show();
}
}
我从我的主叫 activity:
final DriverListFragment fragment = new DriverListFragment();
final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.frameLHaupt, fragment, "FragmentHaupt");
transaction.disallowAddToBackStack(); //main fragment, so no popbackstack
transaction.commit();
我的布局:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:layout_behavior=".pkgActivity.MoveUpwardBehaviour"
tools:context=".pkgTestforend.DriverListFragment">
<ListView
android:id="@+id/listAllDrivers"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
<com.example.dochjavatestimplementation.pkgTestforend.CustomLinearLayout
android:layout_width="match_parent"
android:id="@+id/cusLL"
android:layout_height="match_parent"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/btnOpenDriverAddFragment2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/baseline_person_add_24"
/>
</RelativeLayout>
</com.example.dochjavatestimplementation.pkgTestforend.CustomLinearLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
问题是我的小吃店弹出我的自定义线性布局几秒钟,如果我旋转 phone 而 button/linearlayout 仍然在按钮上,按钮仍然可见,尽管它应该在 oncreate 之后被销毁又接到电话了?
当我旋转 phone:
旋转时按钮状态不应该被破坏是什么原因phone?
将transaction.add(R.id.frameLHaupt, fragment, "FragmentHaupt");
替换为transaction.replace(R.id.frameLHaupt, fragment, "FragmentHaupt");
我有一个简单的片段:
public class DriverListFragment extends Fragment {
public DriverListFragment() {
}
public static DriverListFragment newInstance(String param1, String param2) {
return new DriverListFragment();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_driver_list, container, false);
}
ListView listViewDriver;
DriverListAdapter adapter;
RoomWithRxJavaViewModel viewModel;
FloatingActionButton btnOpenDriverAddFragment2;
CustomLinearLayout customLinearLayout;
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
listViewDriver = view.findViewById(R.id.listAllDrivers);
viewModel = new RoomWithRxJavaViewModel(getActivity().getApplication());
btnOpenDriverAddFragment2 = view.findViewById(R.id.btnOpenDriverAddFragment2);
customLinearLayout = (CustomLinearLayout) view.findViewById(R.id.cusLL);
Snackbar.make(customLinearLayout, "Text to display", Snackbar.LENGTH_LONG).show();
}
}
我从我的主叫 activity:
final DriverListFragment fragment = new DriverListFragment();
final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.frameLHaupt, fragment, "FragmentHaupt");
transaction.disallowAddToBackStack(); //main fragment, so no popbackstack
transaction.commit();
我的布局:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:layout_behavior=".pkgActivity.MoveUpwardBehaviour"
tools:context=".pkgTestforend.DriverListFragment">
<ListView
android:id="@+id/listAllDrivers"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
<com.example.dochjavatestimplementation.pkgTestforend.CustomLinearLayout
android:layout_width="match_parent"
android:id="@+id/cusLL"
android:layout_height="match_parent"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/btnOpenDriverAddFragment2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/baseline_person_add_24"
/>
</RelativeLayout>
</com.example.dochjavatestimplementation.pkgTestforend.CustomLinearLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
问题是我的小吃店弹出我的自定义线性布局几秒钟,如果我旋转 phone 而 button/linearlayout 仍然在按钮上,按钮仍然可见,尽管它应该在 oncreate 之后被销毁又接到电话了?
当我旋转 phone:
旋转时按钮状态不应该被破坏是什么原因phone?
将transaction.add(R.id.frameLHaupt, fragment, "FragmentHaupt");
替换为transaction.replace(R.id.frameLHaupt, fragment, "FragmentHaupt");