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) {
}
}