Фрагмент дублирует при чистке кеша - PullRequest
0 голосов
/ 29 января 2020

Я здесь новенький, мой английский sh немного плох, но я попробую.

В Android У меня есть Fragment внутри Activity с TextView и RecyclerView. Когда Activity открывается, Fragment присоединяется к нему, но когда я выхожу из приложения, удаляю кэш и снова открываю приложение, Fragment дублирует себя.

Без очистки кеш

После очистки кеша

Моя активность:

    public class HomeActivity extends AppCompatActivity implements HomeView {
    DrawerLayout drawerLayout;
    NavigationView navigationView;
    ActionBar actionBar;
    int index;
    FragmentManager fragmentManager;
    FragmentTransaction fragmentTransaction;
    //Fragments
    MyPetsFragment myPetsFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        index = 0;
        setContentView(R.layout.activity_home);
        Toolbar toolbar = findViewById(R.id.IdToolbar);
        drawerLayout = findViewById(R.id.IdDrawerLayout);
        navigationView = findViewById(R.id.IdNavigationView);
        setSupportActionBar(toolbar);
        actionBar = getSupportActionBar();
        actionBar.setHomeAsUpIndicator(R.drawable.icon_menu);
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setDisplayShowTitleEnabled(false);
        LinearLayout NavigationViewMyPets = findViewById(R.id.IdNavigationViewMyPets);
        LinearLayout NavigationViewVetsNearYou = findViewById(R.id.IdNavigationViewVetsNearYou);
        LinearLayout NavigationViewEnergy = findViewById(R.id.IdNavigationViewEnergy);
        LinearLayout NavigationViewPetMatch = findViewById(R.id.IdNavigationViewPetMatch);

        myPetsFragment = new MyPetsFragment();

        if (!myPetsFragment.isAdded()){
            fragmentManager = getSupportFragmentManager();
            fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.add(R.id.IdFragmentContainer, myPetsFragment);
            fragmentTransaction.commit();
        }


        NavigationViewMyPets.setOnClickListener(v -> {
           openFragmentWithoutArguments(myPetsFragment,fragmentTransaction,fragmentManager);
            drawerLayout.closeDrawers();
        });
        NavigationViewVetsNearYou.setOnClickListener(v -> {
            Log.v("Mensaje", "Near");
        });
        NavigationViewEnergy.setOnClickListener(v -> {});
        NavigationViewPetMatch.setOnClickListener(v -> {});

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.mytopmenu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case android.R.id.home:
                drawerLayout.openDrawer(GravityCompat.START);
                return true;

            case R.id.IdMenuItemContactUs:
                return true;

            case R.id.IdMenuItemProfile:
                return true;
        }
        return true;
    }


    @Override
    public void openFragmentWithoutArguments(Fragment fragment, FragmentTransaction fragmentTransaction, FragmentManager fragmentManager) {
        if (!fragment.isAdded()) {
            fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.add(R.id.IdFragmentContainer, fragment);
            fragmentTransaction.commit();
        }
    }

Мой фрагмент XML

<FrameLayout 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"
    tools:context=".view.fragment.MyPetsFragment">

    <ScrollView
        android:id="@+id/IdScrollViewMyPetsFragment"
        android:focusableInTouchMode="true"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:focusableInTouchMode="true"
            android:padding="15dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="My Pets"
                android:textSize="35dp"
                android:textStyle="bold" />

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/IdRecyclerViewMyPets"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />


        </LinearLayout>
    </ScrollView>

    <Button
        android:id="@+id/IdButtonAddPet"
        style="@style/Button_Rounded_Blue"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:layout_marginHorizontal="35dp"
        android:layout_marginTop="30dp"
        android:layout_marginBottom="20dp"
        android:text="Add Pet"
        android:textAllCaps="false"></Button>

</FrameLayout>
...