Tengo un Navigation Drawer el cual instancia fragmentos, el problema que tengo, es que al presionar el botón atrás, el método onBackPressed muestre un alertDialog, pero al entrar a uno de estos fragmentos, al presionar el botón atrás me muestra el alertDialog y no vuelve al activity, y me pregunto como podría realizar la condición para solucionar el problema
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
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.Menu;
import android.view.MenuItem;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.model.LatLng;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener, Valorar.OnFragmentInteractionListener, Reportar.OnFragmentInteractionListener{
GoogleMap map;
MapView mapView;
int i;
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
@Override
protected void onResume() {
super.onResume();
mapView.onResume();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
@Override
protected void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
@Override
protected void onPause() {
super.onPause();
mapView.onPause();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
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.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
mapView = (MapView) findViewById(R.id.MapaPrincipal);
mapView.onCreate(savedInstanceState);
map = mapView.getMap();
map.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
map.setMyLocationEnabled(true);
//safe map build
try {
MapsInitializer.initialize(this);
} catch (Exception e) {
e.printStackTrace();
}
//start point
CameraUpdate center = CameraUpdateFactory.newLatLng(new LatLng(-33.485309,-70.7067445));
CameraUpdate zoom = CameraUpdateFactory.zoomTo(10);
map.moveCamera(center);
map.animateCamera(zoom);
//if start point fail
onMapReady(map);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
new AlertDialog.Builder(this)
.setIcon(R.drawable.cerrar)
.setTitle("Cerrar Aplicación")
.setMessage("Deseas cerrar CicloMapp?")
.setCancelable(true)
.setPositiveButton("Si", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("No", null)
.show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
boolean FragmentTransaction = false;
Fragment fragment = null;
if (id == R.id.AgregarRuta) {
} else if (id == R.id.ValorarRuta) {
fragment = new Valorar();
FragmentTransaction=true;
} else if (id == R.id.ReportarRuta) {
fragment = new Reportar();
FragmentTransaction = true;
} else if (id == R.id.Eventos) {
} else if (id == R.id.Refresco) {
} else if (id == R.id.Leyes) {
}
if(FragmentTransaction){
getSupportFragmentManager().beginTransaction().replace(R.id.content_main,fragment).addToBackStack(null).commit();
item.setChecked(true);
getSupportActionBar().setTitle(item.getTitle());
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
@Override
public void onMapReady(GoogleMap googleMap) {
CameraUpdate center = CameraUpdateFactory.newLatLng(new LatLng(-33.485309,-70.7067445));
CameraUpdate zoom = CameraUpdateFactory.zoomTo(10);
map.moveCamera(center);
map.animateCamera(zoom);
}
@Override
public void onFragmentInteraction(Uri uri) {
}
}
PD: agrego el código