¿Cómo usar el fragmento de diálogo? (showDialog obsoleto) Android
Entiendo que existe esta documentación.
http://developer.android.com/reference/android/app/DialogFragment.html#AlertDialog
pero como nuevo aprendiz de Android / Java no es fácil entender la cantidad de código involucrado al escribir un simple diálogo de alerta que aparece con un mensaje de 2 opciones (sí / no).
Aquí está el código que tengo actualmente en mi archivo MainActivity:
final private int RESET_DIALOG = 0;
private OnClickListener resetButtonListener = new OnClickListener() {
@Override
public void onClick(View v) {
showDialog(RESET_DIALOG);
}
};
protected android.app.Dialog onCreateDialog(int id) {
switch(id) {
case RESET_DIALOG:
AlertDialog.Builder builder = new Builder(this);
return builder
.setMessage("Are you sure you want to reset the count?")
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(MainActivity.this, "Did not reset!", 5).show();
}
})
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(MainActivity.this, "Did Reset!", 5).show();
}
})
.create();
}
return null;
};
Este es mi intento de seguir las instrucciones en el sitio de Android: Archivo de actividad principal:
final private int RESET_DIALOG = 0;
private OnClickListener resetButtonListener = new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, MainDialog.class);
startActivity(intent);
}
};
protected android.app.Dialog onCreateDialog(int id) {
switch(id) {
case RESET_DIALOG:
AlertDialog.Builder builder = new Builder(this);
return builder
.setMessage("Are you sure you want to reset the count?")
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(MainActivity.this, "Did not reset!", 5).show();
}
})
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(MainActivity.this, "Did Reset!", 5).show();
}
})
.create();
}
return null;
};
Luego creó una clase MainDialog: (Estoy realmente perdido en cómo hacer esto correctamente o aplicarlo)
package com.proteintracker;
import android.support.v4.app.DialogFragment;
public class MainDialog extends DialogFragment {
public static MyAlertDialogFragment newInstance(int title) {
MyAlertDialogFragment frag = new MyAlertDialogFragment();
Bundle args = new Bundle();
args.putInt("title", title);
frag.setArguments(args);
return frag;
}
}
No estoy seguro de si se suponía que debía crear una nueva clase para el fragmento y cómo aplicarlo a mi diálogo actual en la pantalla de actividad.