Created
September 13, 2017 12:36
-
-
Save pr00thmatic/b62e0846d6b448868a11143c6ad894e2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// el argumento View es necesario cuando el método es invocado desde el view. | |
public void invocar (View view) { | |
startActivityForResult(new Intent(this, Invocado.class), PEDIDO_DE_MENSAJE); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void enviar (View view) { | |
Intent intento = getIntent(); | |
EditText recipienteNombre = (EditText) findViewById(R.id.nombre); | |
EditText recipienteMensaje = (EditText) findViewById(R.id.mensaje); | |
intento.putExtra("mensaje", recipienteMensaje.getText().toString()); // ewww | |
intento.putExtra("nombre", recipienteNombre.getText().toString()); // eeewwwwww D'X | |
// identifica la respuesta como exitosa. | |
setResult(RESULT_OK, intento); | |
// identifica el tipo de respuesta como respuesta a pedido de mensaje del invocador. | |
finishActivity(Invocador.PEDIDO_DE_MENSAJE); | |
// finaliza la actividad devolviendo la respuesta en el callback de quien la invocó. | |
finish(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Mensaje implements Serializable { | |
public String contenido; | |
public String remitente; | |
public Mensaje (String contenido, String remitente) { | |
this.contenido = contenido; this.remitente = remitente; | |
} | |
public String toString () { | |
return "[" + remitente + "]: " + contenido; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// apetitosa linea de código que envía elegantemente los datos XD | |
intento.putExtra("mensaje", new Mensaje(recipienteMensaje.getText().toString(), | |
recipienteNombre.getText().toString())); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final static int PEDIDO_DE_MENSAJE = 1; | |
// ... | |
@Override | |
public void onActivityResult (int pedido, int resultado, Intent datos) { | |
if (pedido == PEDIDO_DE_MENSAJE) { // tanto pedido como PEDIDO_DE_MENSAJE son numeritos | |
// pero es buena práctica ponerle nombre a estos numeritos en vez de llamarlos | |
// por su numerito XD | |
if (resultado == RESULT_OK) { | |
TextView recipiente = (TextView) findViewById(R.id.recibido); | |
// apetitosa linea de código que recibe elegantemente los datos XD | |
Mensaje recibido = (Mensaje) datos.getSerializableExtra("mensaje"); | |
recipiente.setText("[" + recibido.remitente + "]: " + recibido.contenido); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment