I have an application that has this transition:
A -> B -> C -> D-> C
Upon entering C
, i have to check a flag. Then I have to pass it as intent (let us say intentX = false
) to D
. After doing something in D
, it will then go back to C
after pressing a button.
What i did was just pass again the intentX
with value true, then startActivity C again.
So what happen is that it created another Activity C.
What i want to happen is that i will not have to start a new Activity C, but use the previous C by just calling super.onBackPressed()
. But I cannot pass the new value of the intentX
. Is there other way, to achieve what i want. I might have missed some.
There are some cases where startActivityForResult is not really needed or it is not practical to change all startActivity calls for startActivityForResult.
If the simple case of just starting a previous activity 'again' is needed, my recommendation is: Use the FLAG_ACTIVITY_CLEAR_TOP flag.
Quoting a brief description:
So this example
Where you will get that new intent is defined by which launch mode and which flags where used to start it (in this case our ActivityB).
What you want is
startActivityForResult()
. When you go fromC
toD
, instead of usingstartActivity()
use insteadstartActivityForResult()
. Then when you want to return fromD
toC
you usesetResult()
which can include anIntent
object withextras
to pass back toC
.I don't recommend doing this in
onBackPressed()
if you don't have to because this will not be what the user expects. Instead, you should return with this data with an event such as aButton
click.So, in
C
you will do something likethen in
D
when you are ready to returnthen when you return to
C
you will enter this method (taken from the Docs)