How to call fragments in the DrawerNavigation?

3

I created a new project in android studio and chose to use navigationDrawer , I created my screens now I would like to call them. How do I implement this method and toggle windows in the menu of an application using:

public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();
    Fragment fragment = null;
    if (id == R.id.servic) {
       fragment = new Cronograma();
    } else if (id == R.id.nav_camera)  {
        fragment = new Localizacao();
    } else if (id == R.id.nav_gallery) {
        fragment = new Sobre_aplicativo();
    } else if (id == R.id.nav_share) {
        finish();
    }

    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.mainFrame, fragment);
    transaction.addToBackStack(null);
    transaction.commit();
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

ACTIVITY MAIN

<?xml version="1.0" encoding="utf-8"?>

<include
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

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


<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer" />

CONTENT MAIN:

<?xml version="1.0" encoding="utf-8"?>

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

<ListView
    android:id="@+id/listView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true" />

APP BAR MAIN:

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_dialog_email" />

ACTIVITY MAIN DRAWER

<?xml version="1.0" encoding="utf-8"?>

<group android:checkableBehavior="single">
    <item
        android:id="@+id/servic"
        android:icon="@drawable/servico"
        android:title="Serviços" />
    <item
        android:id="@+id/nav_camera"
        android:icon="@drawable/pref"
        android:title="A prefeitura" />
    <item
        android:id="@+id/nav_gallery"
        android:icon="@drawable/local"
        android:title="Localização" />

</group>

<item android:title="Informações">
    <menu>

        <item
            android:id="@+id/nav_share"
            android:icon="@drawable/dev"
            android:title="Sobre o aplicativo" />
        <item
            android:id="@+id/nav_manage"
            android:icon="@drawable/exit"
            android:title="Sair" />
    </menu>
</item>

    
asked by anonymous 07.06.2016 / 04:10

1 answer

3

The ideal thing is that the layout of your activity has something that serves as a container, it is very common to use a FrameLayout for these cases.

So, create an activity method that does the following:

String backStateName = fragment.getClass().getName();
        FragmentManager manager = getSupportFragmentManager();
        manager.popBackStackImmediate(backStateName, FragmentManager.POP_BACK_STACK_INCLUSIVE);
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(container (id do container), fragment);
        transaction.addToBackStack(backStateName);
        transaction.commit();

This method should be instantiated as an instance of the fragment you want to open, eg:

  if (id == R.id.nav_camera) {
       abreFragmento(new CameraFragment());
  }

So, your Activity's fragmentManager will replace what's in the container with the fragment you've instantiated.

Extra:

In this method, fragmentManager saves the fragments that were opened in order, if you use openFragment (new OtherFragment ()) and another fragment is already open, it goes into onStop () and you can go back to it using the fragmentManager.popBackStack() method.

    
07.06.2016 / 04:55