Android,片段加载在其他片段onBack按下

时间:2014-12-09 07:17:20

标签: java android android-fragments android-fragmentactivity fragmentmanager

在我的应用中,使用导航绘图进行导航可以打开碎片。直到我按下后退按钮,然后片段开始加载到另一个上面。 (比如堆积起来)。

怎么能阻止这种快乐。

这是我的导航抽屉功能:

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

这是我的Navigaton 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>

0 个答案:

没有答案