androidandroid-fragmentsandroid-fragmentactivityondestroyfragment-oncreateview

Button of destroyed fragment still visible


I've got a simple fragment:

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();

    }

}

Which I call from my main 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();

My layout:

<?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>

The issue is that my snackbar pops up my customlinearlayout for few seconds and if i rotate the phone while the button/linearlayout is still up the button remains visible, altough it should be destroyed since oncreate gets called again?

default state

and when i rotate the phone:

rotateed stte

What can the cause be shouldnt the button state be destroyed when I rotate the phone?


Solution

  • Replace transaction.add(R.id.frameLHaupt, fragment, "FragmentHaupt"); with transaction.replace(R.id.frameLHaupt, fragment, "FragmentHaupt");