Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/183.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 安卓,将碎片加载到其他碎片上,然后按Back_Java_Android_Android Fragments_Android Fragmentactivity_Fragmentmanager - Fatal编程技术网

Java 安卓,将碎片加载到其他碎片上,然后按Back

Java 安卓,将碎片加载到其他碎片上,然后按Back,java,android,android-fragments,android-fragmentactivity,fragmentmanager,Java,Android,Android Fragments,Android Fragmentactivity,Fragmentmanager,在我的应用程序中,使用导航绘图导航可以很好地打开碎片。在我按下后退按钮之前,碎片开始在另一个上面加载。(像叠起来一样) 如何才能阻止这种情况发生 以下是我的导航抽屉功能: private void setFragmentList(int position) { //TODO set Fragments here Fragment mFragment = null; mFragmentManager = getSupportFragmentManager();

在我的应用程序中,使用导航绘图导航可以很好地打开碎片。在我按下后退按钮之前,碎片开始在另一个上面加载。(像叠起来一样)

如何才能阻止这种情况发生

以下是我的导航抽屉功能:

private void setFragmentList(int position) { //TODO set Fragments here

    Fragment mFragment = null;      
    mFragmentManager = getSupportFragmentManager();

    switch (position) {
    case 1: //Home      
        mFragment = new HomeFragment();
        break;

    case 2: //Login         
        mFragment = new LoginFragment();
        break;

    case 3: //Logout
        DoUserLogout();
        break;

    case 4: //Post New Product
        mFragment = new PostProductFragment();
        break;

    case 5:     
        mFragment = new UserProfileFragment();
        break;



    }//end switch


    if (mFragment != null){
        setTitleFragments(mLastPosition);
        mNavigationAdapter.resetarCheck();      
        mNavigationAdapter.setChecked(position, true);          
        mFragmentManager.beginTransaction()
        .replace(R.id.content_frame, mFragment)
        .addToBackStack(null)
        .commit();
    }
}//end setFragmentList
下面是如何从片段加载新片段:

Fragment mFragment = null;
mFragmentManager = getFragmentManager();
mFragment = new EditUserProfileFragment().newInstance("Edit Profile");

if (mFragment != null){
    mFragmentManager.beginTransaction()
    .replace(R.id.content_frame, mFragment)
    .addToBackStack(null).commit();
}
以下是我的XML导航:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/gray_navegation" >

    <android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/layoutDrawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="wrap_content"
            android:layout_height="match_parent" >
        </FrameLayout>

        <RelativeLayout
            android:id="@+id/relativeDrawer"
            android:layout_width="260dp"
            android:layout_height="match_parent"
            android:background="@color/navigation_items"
            android:layout_gravity="start"
            android:orientation="vertical" >
            <!-- android:layout_gravity="start" -->

            <LinearLayout
                android:id="@+id/userContent"
                android:layout_width="260dp"
                android:layout_height="wrap_content"
                android:background="@color/drawer_orange_dark"
                android:gravity="center_vertical"
                android:paddingRight="10dp" >

                <RelativeLayout
                    android:id="@+id/userDrawer"
                    android:layout_width="match_parent"
                    android:layout_height="80dp">



                        <TextView
                            android:id="@+id/txt_user_name_drawer"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="15dp"
                            android:layout_marginLeft="5dp"
                            android:layout_marginRight="5dp"
                            android:layout_toRightOf="@+id/ImgDrawer"
                            android:text="@string/app_name"
                            android:textAllCaps="true"
                            android:textAppearance="?android:attr/textAppearanceMedium"
                            android:textColor="@color/white" />

                        <TextView
                            android:id="@+id/txt_user_lastname_drawer"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_alignLeft="@+id/txt_user_name_drawer"
                            android:layout_below="@+id/txt_user_name_drawer"
                            android:text="@string/app_name"
                            android:textAllCaps="true"
                            android:textColor="@color/white"
                            android:textSize="12sp" />

                        <ImageView
                            android:id="@+id/ImgDrawer"
                            android:layout_width="50dp"
                            android:layout_height="50dp"
                            android:layout_alignParentLeft="true"
                            android:layout_centerVertical="true"
                            android:layout_marginLeft="14dp"
                            android:contentDescription="@string/app_name"
                            android:src="@drawable/ic_launcher" />



                </RelativeLayout>
            </LinearLayout>

            <View
                android:id="@+id/viewSeparator"
                android:layout_width="match_parent"
                android:layout_height="2dp"       
                android:layout_below="@+id/userContent"         
                android:background="@color/drawer_brown" />

            <ListView
                android:id="@+id/listDrawer"
                android:layout_width="260dp"
                android:layout_height="match_parent"
                android:layout_gravity="start"
                android:layout_below="@+id/viewSeparator"                
                android:background="@color/navigation_items"
                android:cacheColorHint="@color/transparent"
                android:choiceMode="singleChoice"
                android:divider="@android:color/transparent"
                android:dividerHeight="1dp" />
        </RelativeLayout>
    </android.support.v4.widget.DrawerLayout>
</RelativeLayout>


删除此行
。addToBackStack(null)
;Muhammad Babar,如果我删除.addToBackStack(null),那么当在应用程序中导航时,如果按back bt,它将不会返回预览页面(片段),您可以发布片段的xml文件ie布局,然后仅将预览页面片段添加到backstack!是的,因为你没有设置它。