Java 当我清理缓存时,片段重复

Java 当我清理缓存时,片段重复,java,android,xml,android-layout,android-recyclerview,Java,Android,Xml,Android Layout,Android Recyclerview,我是新来的,我的英语有点差,但我会努力的 在安卓系统中,我在活动中有一个片段,带有文本视图和回收视图。 当活动打开时,片段已附加到它,但当我退出应用程序并删除缓存并再次打开应用程序时,片段会复制自身 我的活动: 公共类HomeActivity扩展AppCompatActivity实现HomeView{ 抽屉布局抽屉布局; 导航视图导航视图; ActionBar ActionBar; 整数指数; 碎片管理器碎片管理器; 零碎交易零碎交易; //碎片 MyPetsFragment MyPetsF

我是新来的,我的英语有点差,但我会努力的

在安卓系统中,我在
活动
中有一个
片段
,带有
文本视图
回收视图
。 当
活动
打开时,
片段
已附加到它,但当我退出应用程序并删除缓存并再次打开应用程序时,
片段
会复制自身

我的活动:

公共类HomeActivity扩展AppCompatActivity实现HomeView{
抽屉布局抽屉布局;
导航视图导航视图;
ActionBar ActionBar;
整数指数;
碎片管理器碎片管理器;
零碎交易零碎交易;
//碎片
MyPetsFragment MyPetsFragment;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
指数=0;
setContentView(R.layout.activity_home);
Toolbar Toolbar=findviewbyd(R.id.IdToolbar);
抽屉布局=findViewById(R.id.IDDroperLayout);
navigationView=findViewById(R.id.IdNavigationView);
设置支持操作栏(工具栏);
actionBar=getSupportActionBar();
actionBar.setHomeAsUpIndicator(R.drawable.icon_菜单);
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=新的myPetsFragment();
如果(!myPetsFragment.isAdded()){
fragmentManager=getSupportFragmentManager();
fragmentTransaction=fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.IdFragmentContainer,myPetsFragment);
fragmentTransaction.commit();
}
NavigationViewMyPets.setOnClickListener(v->{
openFragmentWithoutArguments(myPetsFragment、fragmentTransaction、fragmentManager);
抽屉布局;
});
NavigationViewVetsnaryou.setOnClickListener(v->{
Log.v(“Mensaje”、“Near”);
});
NavigationViewEnergy.setOnClickListener(v->{});
NavigationViewPetMatch.setOnClickListener(v->{});
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
getMenuInflater().充气(R.menu.mytopmenu,menu);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
开关(item.getItemId()){
案例android.R.id.home:
抽屉布局。打开抽屉(重力compat.START);
返回true;
案例R.id.IdMenuItemContactUs:
返回true;
案例R.id.IdMenuItemProfile:
返回true;
}
返回true;
}
@凌驾
public void openFragmentWithoutArguments(fragmentfragment、FragmentTransaction FragmentTransaction、FragmentManager FragmentManager){
如果(!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>


什么是被重写的方法“
openFragmentWithoutArguments
”,它重写了什么?@LucaMurra在点击导航视图中的一个菜单项时调用它,并在点击的菜单项上打开一个片段解除挂起,但为什么会有
@override
注释?@LucaMurra因为它是从inte实现的请使用标准的局部变量表示法:如您所见,“代码> >导航视图MPETPES/代码>和类似的代码是由堆栈溢出颜色格式化程序错误解释的,这是因为第一个字母是资本,当它应该很小的时候。