return to the main activity in navigation draw

2

Good morning. When I launch my application, the first screen that shows me is the main container of the fragments that I have in the navigation draw. When selecting the items that take me to the different fragments I could not find a way to return to the first screen. I'm sure an attempt is absurd, but I have no idea how to return. Thank you very much for your answers.

public boolean onNavigationItemSelected(MenuItem item) {
    FragmentManager fm = getSupportFragmentManager();
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_menu) {
        // Handle the camera action
        ///volver a la pantalla principal
    }
    else if (id == R.id.nav_pedido) {
        // Handle the camera action
        fm.beginTransaction().replace(R.id.contenedor,new Pedido()).commit();
    } else if (id == R.id.nav_stok) {
        fm.beginTransaction().replace(R.id.contenedor,new Stock()).commit();
    } else if (id == R.id.nav_deuda) {
        fm.beginTransaction().replace(R.id.contenedor,new Deuda()).commit();
    } else if (id == R.id.nav_despacho) {
        fm.beginTransaction().replace(R.id.contenedor,new Despacho()).commit();
    } else if (id == R.id.nav_ventas) {
        fm.beginTransaction().replace(R.id.contenedor,new Ventas()).commit();
    } else if (id == R.id.nav_ruta) {
        fm.beginTransaction().replace(R.id.contenedor,new Ruta()).commit();
    }

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

main xml

<include
    layout="@layout/app_bar_vendedor"
    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_vendedor"
    app:menu="@menu/vendedor_drawer" />

xml app bar

<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_vendedor" />

xml nav header

<TextView
    android:id="@+id/textView5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView"
    android:textSize="30sp"
    tools:ignore="HardcodedText" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/nav_header_vertical_spacing"
    android:text="Android Studio"
    android:textAppearance="@style/TextAppearance.AppCompat.Body1"
    tools:ignore="HardcodedText" />

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="[email protected]"
    tools:ignore="HardcodedText" />

xml drawer

<group android:checkableBehavior="single">
    <item
        android:id="@+id/nav_menu"
        android:checkable="false"
        android:title="Menu"
        tools:ignore="HardcodedText" />
</group>
<item android:title="Transacciones"
    tools:ignore="HardcodedText">
    <menu>
        <item
            android:id="@+id/nav_pedido"
            android:icon="@drawable/ic_menu_send"
            android:title="Ingrese Pedido"
            tools:ignore="HardcodedText" />
    </menu>
</item>

<item android:title="Consultas"
    tools:ignore="HardcodedText">
    <menu>
        <item
            android:id="@+id/nav_stok"
            android:icon="@drawable/ic_menu_send"
            android:title="Stock" />
        <item
            android:id="@+id/nav_deuda"
            android:icon="@drawable/ic_menu_send"
            android:title="Deuda" />
        <item
            android:id="@+id/nav_despacho"
            android:icon="@drawable/ic_menu_send"
            android:title="Despacho" />
        <item
            android:id="@+id/nav_ventas"
            android:icon="@drawable/ic_menu_send"
            android:title="Avance de Ventas"
            tools:ignore="HardcodedText" />
        <item
            android:id="@+id/nav_ruta"
            android:icon="@drawable/ic_menu_send"
            android:title="Hoja de Ruta" />
    </menu>
</item>

    
asked by ICRUZ 02.10.2017 в 16:28
source

3 answers

0

When reviewing your code I see that you load Fragments in your main Activity, but the main one does not load any Fragment at the beginning, you can use this method using onBackPressed() , which returns to Fragment previous using popBackStackImmediate () , if it is found, when you reach the MainActivity You can implement the exit of the Activity by finish() .

@Override
public void onBackPressed() {

       Fragment currentFragment = getSupportFragmentManager()
            .findFragmentById(R.id.contenedor);

        if (getSupportFragmentManager().getBackStackEntryCount() > 0) {               
            getSupportFragmentManager().popBackStackImmediate();
        }/*else{
            finish();  //Sale de Activity principal.
        }*/


}
    
answered by 02.10.2017 в 17:40
0

In OnCreate of the MainActivity you must add ( add ) the fragment with which you are going Start the application:

FragmentMenu fragment = new FragmentMenu();
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.add(R.id.contenedor, fragment, "iniciofragment");
        ft.commit();

FragmentMenu or whatever your initial fragment is called.

And you must also add that same fragment of start in onNavigationItem:

public boolean onNavigationItemSelected(MenuItem item) {
    FragmentManager fm = getSupportFragmentManager();
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_menu) {
        ///volver a la pantalla principal
        fm.beginTransaction().replace(R.id.contenedor, FragmentMenu()).commit();
    }
    else if (id == R.id.nav_pedido) ...
    
answered by 02.10.2017 в 23:29
0

To return to the main menu.

import android.support.v4.app.NavUtils;

if (id == R.id.nav_menu) {
        // Handle the camera action
       ///volver a la pantalla principal
       NavUtils.navigateUpTo(this,  new Intent(this, MiContenedorPrincipal.class));
       return true;

To navigate to the top try adding:

@Override
public void onCreate(Bundle savedInstanceState) {
    ...
    getActionBar().setDisplayHomeAsUpEnabled(true);
}

.....

if (id == R.id.nav_menu) {
        NavUtils.navigateUpFromSameTask(this);
        return true;
    }


        }
    
answered by 02.10.2017 в 19:48