Navigationdrawer片段中的后退按钮

时间:2016-07-20 11:40:50

标签: java android android-fragments navigation-drawer onbackpressed

我创建了一个导航抽屉,其中包含一个活动和多个片段,如frag a,frag b,frag c,...如果我单击片段c中的后退按钮,它应该变为frag a。问题是返回操作不起作用。可以告诉我如何在下面的代码中做到这一点吗?

if (!$(this).is(':checked')) {

3 个答案:

答案 0 :(得分:1)

更改此行

fragmentManager.beginTransaction()
                 .replace(R.id.frame_content, fragment)
                .addToBackStack(null)
                .commit();

到这个

fragmentManager.beginTransaction()
                 .add(R.id.frame_content, fragment)
                .addToBackStack(null)
                .commit();

addToBackStack适用于add

replace函数删除了以前的片段并将新片段放在你的后台上,只有一个片段。因此,使用add函数将以前的片段保留在堆栈中。

答案 1 :(得分:0)

这应该可以解决问题

public void onBackPressed()
{
    FragmentManager fm = getActivity().getSupportFragmentManager();
    fm.popBackStack();
}

如果没有,请查看how to implement your own backstack

希望这有助于:)

答案 2 :(得分:0)

//将此代码放入MainActivity

@SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        Fragment fragment;
        int id = item.getItemId();
       // String title = getString(R.string.app_name);

        if (id == R.id.register) {
            fragment=new Register();
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            ft.replace(R.id.mainframe,fragment);
            ft.addToBackStack(null);
            ft.commit();
         //   title="Proviso Intech :: Registration";


        } else if (id == R.id.job) {
            fragment=new job();
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            ft.replace(R.id.mainframe,fragment);
            ft.addToBackStack(null);
            ft.commit();
          //  title="Proviso Intech :: Job Search";

        }
        else if(id==R.id.home){
            fragment=new home();
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            ft.replace(R.id.mainframe,fragment);
            ft.addToBackStack(null);
            ft.commit();
         //   title="Proviso Intech :: Home";


        }
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

这是我的content_main.xml

  <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:id="@+id/main"

    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.intech.proviso.proviso.MainActivity"
    tools:showIn="@layout/app_bar_main">


    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:id="@+id/mainframe"></FrameLayout>
</RelativeLayout>

//这里是activity_main_drawer.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"

    >

    <group android:checkableBehavior="single">
        <item
            android:id="@+id/home"
            android:icon="@drawable/common_full_open_on_phone"
            android:checkable="true"
            android:title="Home" />
        <item
            android:id="@+id/register"
            android:icon="@drawable/ic_menu_send"
            android:title="Register" />
        <item
            android:id="@+id/job"
            android:icon="@drawable/ic_menu_manage"
            android:title="Job Posts" />


    </group></menu>