2

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");
    }
}
Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95
Kingraj
  • 25
  • 1
  • 9

3 Answers3

1

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.

Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95
  • If i put add its going back..then Main fragment will display..If you again click back button..app is not exist..its displaying FrameLayout – Kingraj Jul 20 '16 at 12:11
0

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 :)

Community
  • 1
  • 1
Mr.7
  • 2,292
  • 1
  • 20
  • 33
  • while doing back operation... if you come from last fragment to home fragment...in first fragment action bar its showing last fragment title – Kingraj Jul 22 '16 at 12:22
  • @Kingraj you can change your Actionbar title from fragment by `((MainFragmentActivity) getActivity()).setActionBarTitle(YOUR_TITLE);` – Mr.7 Jul 22 '16 at 12:33
  • If you want yo change it from your activity, use this `getSupportActionBar().setTitle(title);` – Mr.7 Jul 22 '16 at 12:35
  • In fragment where should i add this...((MainFragmentActivity) getActivity()).setActionBarTitle(YOUR_TITLE); – Kingraj Jul 22 '16 at 12:52
  • within the `onCreateView` method – Mr.7 Jul 22 '16 at 12:55
0

//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>
Umar Ata
  • 4,170
  • 3
  • 23
  • 35