- How many times have you needed to return a result from one
Activityto another? - A lot of times, actually, I don't remember any app that I've writen without that functionality.
- was that difficult? How did you do it?
- It wasn't, in fact it was pretty easy. Android has a method
startActivityForResult(Intent, int)that allows us to launch anActivitywaiting for its result in a method calledonActivityResult(int, int, Intent), no mistery there. - Well, and what happens when there is more than one
Activityinvolved in the process? - ah, it is very easy too, when the second
Activityof the flow is launched you have to add a the flagIntent.FLAG_ACTIVITY_FORWARD_RESULTto theIntentand then it will be your nextActivitywho will be in charge to callsetResultto deliver the result. - is that all?
- Well, it is not, you need to call
Activity.finishin the firstActivityof the flow, else when the user ends in the second one, he will get back to the first one and he will need to click back to leave the flow and receive the result. - That seems a little bit... odd?
- But you can do
Activity.finishas I said before, in that way when the user finishes the secondActivitythe flow will be finished as well. - and, what happens if the user clicks
backin the lastActivityof the flow? - That it leaves the flow.
- But then the user loses all the data provided during the flow, is that right?
- Yes.
- And what if instead of a flow with 2 activities we have a flow with 5, 6 or even 10 activities?
- Then the user would need to start the flow from the beginning.
- Well, that was the point from the beginning, I've also faced that problem and so far, the solution presented below is the one that I like more.
How?
This is very easy and in only a few steps.- First we need to start the first
Activityof the flow likestartActivityForResult:
Intent intent = new Intent(this, ProfileNameStepActivity.class); startActivityForResult(intent, PROFILE_REQUEST_CODE_FLOW);- Then we wait the result like we always do when there is only one
Activityin the flow.
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == PROFILE_REQUEST_CODE_FLOW && resultCode == RESULT_OK) { Profile profile = ExtrasUtils.getProfile(data); onProfileCompleted(profile); } else { super.onActivityResult(requestCode, resultCode, data); } }- Also in the last
Activityof the flow we add (like always):
Intent intent = new Intent(); intent.putExtra(ExtrasUtils.EXTRA_PROFILE, new Profile.Builder().withName(name).withAddress(address).build()); setResult(RESULT_OK, intent); finish();So far we haven't done anything special thing, this code we've written hundreds of times.This is the interesting part...- Now we start any other
Activityinvolved in the flow withstartActivityForResult, while this time therequestCodeis not important because we don't wait for it result:
/* We can pass any number less the ones that we listen here, in this case ADDRESS_REQUEST_CODE_FLOW (unless we want to start a sub-flow, I'll explain that later */ Intent intent = new Intent(this, ProfileFinalStepActivity.class); startActivityForResult(intent, 0);- Finally we need this piece of code in our
BaseActivityand all our activities will inherit from it.
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK) { setResult(resultCode, data); finish(); } }This last part of the code is the one that does the magic, if you pay attention, we have started all our activities in the flow withstartActivityForResult, so when the nextActivityis finished our activities will receive a call inonActivityResult, and we do check if we already got a result there from the nextActivity, so if theActivityreceives the result, we set the result for the currentActivityand the previous one will do the same until anyActivityhas itsonActivityResultoverriden and it is waiting for therequestCode.This same technique can be used to include sub-flows inside a flow, imagine for example that the user can do shopping in your application and before the user pays it has to provide some basic info like the name or the address (or a complete profile).The same code that we have used to start/finish a flow, we can use it in any step of the flow to add a sub-flow and it will work just in the same way.What have we achieved with this?
- Now we are able to start/finish a flow in the same place.
- We can deliver a result of a flow (group of activities) very easy.
- We can add additional flows inside a flow easily.
Mostrando entradas con la etiqueta onActivityResult. Mostrar todas las entradas
Mostrando entradas con la etiqueta onActivityResult. Mostrar todas las entradas
12 de abril de 2016
Android - Flow result instead of Activity result or how to get a result between activities
Etiquetas:
Activity,
Activity Result,
Android,
Development,
FLAG_ACTIVITY_FORWARD_RESULT,
Flow,
Flow Result,
onActivityResult,
Result,
startActivityForResult
9 de abril de 2016
Android - Flow result en lugar de Activity result o como obtener un resultado entre actividades
- ¿Cuantas veces te has encontrado que necesitas entregar un resultado de una
Activitya otra? - Muchas, no recuerdo ninguna aplicación donde no lo haya tenido que hacer.
- ¿Te resultó difícil? ¿Como lo hiciste?
- No, de hecho fue muy fácil, Android siempre ha tenido un método
startActivityForResult(Intent, int)para lanzar unaActivityde la que esperas un resultado y otro métodoonActivityResult(int, int, Intent)para recibirlo, no tiene ningún misterio. - Bien ¿y que pasa si hay más de una
Activityimplicada en el proceso? - Ah, también es muy fácil, cuando lanzas la segunda
Activityimplicada en el flow lo haces añadiendo el flagIntent.FLAG_ACTIVITY_FORWARD_RESULTalIntenty entonces será tu siguienteActivityla encargada de llamar asetResultpara entregar el resultado. - ¿Eso es todo?
- Bueno, en realidad no, en realidad tienes que hacer un
Activity.finishde la primeraActivitydel flow porque sino cuando el usuario termine la segunda, volverá a la primera y tendrá que salir manualmente de esta para recibir el resultado. - Mmmm... ¿eso no es muy elegante no?
- Bueno, puedes llamar a
Activity.finishcomo he dicho antes, de esta forma cuando el usuario termina en la segundaActivity, va directamente a la que está esperando el resultado. - ¿Y que pasa si el usuario pulsa
backcuando está al final del flow? - Que se sale del flow.
- Pero entonces pierde toda la información que tenía entre medias ¿no?
- Si.
- ¿Y si en lugar de tener un flow de 2 actividades tienes un flow de 5 o 6 o incluso de 10?
- Pues el usuario tendría que comenzar el flow desde el principio.
- Bien, ahí es donde quería llegar, yo también me he enfrentado a este problema y de momento, esta es la solución que mas me hag gustado.
¿Como hacerlo?
Esto es bastante sencillo de conseguir y lo más importante, con poco código:- Primero necesitamos lanzar la primera
Activitydel flow constartActivityForResult:
Intent intent = new Intent(this, ProfileNameStepActivity.class); startActivityForResult(intent, PROFILE_REQUEST_CODE_FLOW);- Y esperamos el resultado como hariamos si el flow tuviese una sola
Activity.
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == PROFILE_REQUEST_CODE_FLOW && resultCode == RESULT_OK) { Profile profile = ExtrasUtils.getProfile(data); onProfileCompleted(profile); } else { super.onActivityResult(requestCode, resultCode, data); } }- También en la última
Activitydel flow añadiremos:
Intent intent = new Intent(); intent.putExtra(ExtrasUtils.EXTRA_PROFILE, new Profile.Builder().withName(name).withAddress(address).build()); setResult(RESULT_OK, intent); finish();De momento no hemos hecho nada especial, esté código lo hemos escrito cientos de veces cada vez que lanzamos unaActivityesperando un resultado de ella.Esta es la parte interesante...- Ahora lanzamos el resto de actividades del flow con
startActivityForResulttambién, aunque en este caso elrequestCodeno es importante ya que no esperamos su resultado:
/* Podemos pasar cualquier número como request code aquí porque no lo escucharémos (a no ser que queramos lanzar un sub-flow, después explico que sería un sub-flow y como incluirlo) */ Intent intent = new Intent(this, ProfileFinalStepActivity.class); startActivityForResult(intent, 0);- Finalmente necesitamos esta pieza de código en nuestra
BaseActivityde la cual heredan todas nuestras actividades.
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK) { setResult(resultCode, data); finish(); } }Esta última parte de código, es la que realmente hace la magia. Si te fijas, como hemos lanzado todas nuestras actividades constartActivityForResult, cuando estas terminen laActivityanterior será llamada enonActivityResult, y lo que estamos haciendo es comprobar si laActivityde la que viene ha añadido el resultado ya, si lo ha hecho, pasamos el resultado a laActivityanterior y esto se hará progresivamente hasta que algunaActivityhaya sobreescrito el métodoonActivityResulty esté actualmente esperando por elrequestCode.Está misma técnica puede ser utilizada para incluir sub-flows dentro de un flow, imagina por ejemplo que tu aplicación permite al usuario realizar compras y que para realizar esa compra el usuario tiene que proveer alguna información básica como el nombre y la dirección (o un perfil entero).El mismo código que hemos utilizado para empezar/acabar un flow, podemos incluirlo en cualquier paso del flow para añadir un sub-flow y funcionará exactamente igual.¿Que hemos conseguido con esto?
- Ser capaz de empezar y acabar un flow en el mismo sitio.
- Entregar el resultado de un flow (grupo de actividades) de una forma sencilla.
- Posibilidad de añadir flows adicionales dentro del flow.
Etiquetas:
Activity,
Activity Result,
Android,
Development,
FLAG_ACTIVITY_FORWARD_RESULT,
Flow,
Flow Result,
onActivityResult,
Result,
startActivityForResult
Suscribirse a:
Entradas (Atom)