android: NavigationDrawer, return to the main fragment and not to the previous activity

1

I'm doing an app which has a Login and then enters the activity with the NavigationDrawer , I've already defined a Fragment so that with the interface starts, however when I find myself in another Fragment of the navigation and back it returns me to the Login and not to the Fragment Principal.

I require: * Start the 'NavigationDrawer' with the fragment item that I defined as main. * Achieve that being in the Fragment X, return to the main fragment and not the Login.

package utxj.proyecto.ixchelap;


import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;

public class NavLateral extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener,
        MenuPrincipal.OnFragmentInteractionListener, miEstatus.OnFragmentInteractionListener, misDatos.OnFragmentInteractionListener,
        misCitas.OnFragmentInteractionListener, miHistorial.OnFragmentInteractionListener, misTratamientos.OnFragmentInteractionListener,
        misEstadisticas.OnFragmentInteractionListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nav_lateral);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        /******************************INICIAR CON ESTE FRAGMENT**************************************************/
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.navContent, new miEstatus()).commit();
        /******************************INICIAR CON ESTE FRAGMENT**************************************************/

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.nav_lateral, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement


        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        Fragment fragment = null;
        boolean FragmentSeleccionado = false;
        Fragment fragmentPrincipal = new miEstatus();

        if (id == R.id.nav_estatus) {
            // Handle the camera action
            fragment = new miEstatus();
            FragmentSeleccionado = true;

            getSupportActionBar().setTitle("Estatus");
        } else if (id == R.id.nav_datos) {
            fragment = new misDatos();
            FragmentSeleccionado = true;
            getSupportActionBar().setTitle("Mis Datos");
        } else if (id == R.id.nav_citas) {
            fragment = new misCitas();
            FragmentSeleccionado = true;
            getSupportActionBar().setTitle("Citas");
        } else if (id == R.id.nav_historial) {
            fragment = new miHistorial();
            FragmentSeleccionado = true;
            getSupportActionBar().setTitle("Historia Clínica");
        } else if (id == R.id.nav_tratamientos) {
            fragment = new misTratamientos();
            FragmentSeleccionado = true;
            getSupportActionBar().setTitle("Tratamientos");
        } else if (id == R.id.nav_estadisticas) {
            fragment = new misEstadisticas();
            FragmentSeleccionado = true;
            getSupportActionBar().setTitle("Estadísticas");
        } else if(id == R.id.nav_cerrarsesion){
            Log.i("Accion", "Cerro Sesion");
        }

        if (FragmentSeleccionado){
            getSupportFragmentManager().beginTransaction().replace(R.id.navContent, fragment).commit();
            item.setChecked(true);
        }

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

    @Override
    public void onFragmentInteraction(Uri uri) {

    }
}
    
asked by Noé Aguilar Farías 29.07.2017 в 00:19
source

1 answer

0

The onBackPressed() method should look like this

@Override
public void onBackPressed() {
    FragmentManager fragmentManager = getSupportFragmentManager();
    Fragment currentFragment = fragmentManager.findFragmentById(R.id.navContent);
    if (currentFragment instanceof miEstatus) {
        // Si ya estamos en el fragment principal, entonces dejar que 
        // se maneje el evento de la forma predeterminada
        super.onBackPressed();
    } else {
        // Vamos al fragment inicial.
        fragmentManager.beginTransaction().replace(R.id.navContent, new miEstatus()).commit();
        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setCheckedItem(R.id.nav_estatus);
    }
}

I suggest you use instance variables so that you do not have to unnecessarily repeat calls to methods like findById(...) . For example, declare navigationView as an instance variable and initialize it in onCreate(...) .

    
answered by 29.07.2017 в 07:04