Kingraj
Kingraj

Reputation: 25

back button in Navigationdrawer fragment

I created a navigation drawer with one activity and multiple fragments like frag a, frag b, frag c, ... If I click back button in fragment c it should come to frag a. The problem is back operation is not working. Can any tell me how to do it in the below code?

private void displayView(int position) {
    Fragment fragment=null;

    switch (position) {
        case 0:
            fragment = new FragmentA();
            break;
        case 1:
            fragment = new FragmentB();
            break;
        case 2:
            fragment = new FragmentC();
        default:
            break;
    }

    if (fragment != null) {
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
                 .replace(R.id.frame_content, fragment)
                .addToBackStack(null)
                .commit();
        menulistView.setItemChecked(position, true);
        menulistView.setSelection(position);
        setTitle(navMenuTitles[position]);
    } else {
        Log.e("MainActivity", "Error");
    }
}

Upvotes: 2

Views: 303

Answers (3)

Umar Ata
Umar Ata

Reputation: 4258

//place this code in your 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;
    }

and this is my 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>

//here is the 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>

Upvotes: 0

Mr.7
Mr.7

Reputation: 2713

This should do the trick

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

if not, look at how to implement your own backstack

hope this helps :)

Upvotes: 0

Harshad Pansuriya
Harshad Pansuriya

Reputation: 20910

Change this line

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

to this

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

addToBackStack works with add.

replace function removes previous fragment and places new fragment so on your back-stack there is only one fragment all the time. So use add function to keep previous fragments on stack.

Upvotes: 1

Related Questions