To describe the situation, say that I have 3 activities: A, B, and C
and there is a button in activity A which starts activity B, and there is a button in activity C when it is clicked, it should send a result from activity C to activity A
My Question is...Is there a way to pass a result from C to A? If there is a way, what it is?
Note: It would be good if the way you give uses the methods startActivityForResult(...) and onActivityResult(...)
Thank you in advance
If you just using simple types like String objects you can use Bundle and supplementary variables in B,C activities. And transfer it from C->B->A using onActivity result. Or you could use Shared preferences.
There's a flag of Intent called FLAG_ACTIVITY_FORWARD_RESULT. Call:
Intent intent = new Intent(this, ActivityB.class);
startActiivtyForResult(intent);
when starting Activity B (by calling startActivityForResult(intent)).
When opening C, call:
Intent intent = new Intent(this, ActivityB.class);
intent.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
startActiivtyForResult(intent);
finish();
which means: open C, close B. Then, when closing Acitivity C, call:
setResult(123);
finish();
the result from C will go back to A.
You can use Intent to transfer data if you are navigating from Activity C to Activity A. Otherwise, I would suggest using an Interface, and then pass the data as parameter in a callback method present in Activity A. You said you wanted answers like onActivityResult, for that I guess the Activity C should exit or something to invoke onActivityResult in the Activity A
Related
I have a use-case where my Activity A will start other Activity B of some SDK, here Activity B is a transparent activity hence allowing me to still interact with A. Now from A, I start another activity C by using startActivityForResult() and from C, I am sending back the result using setResult(). But I am not able to get the result back in onActivityResult() of A.
Point to be noted here is that Activity B from other SDK is having launchmode set as "singleTask" in androidManifest file.
How I can get result back from C to A in this case?
Here is the code used in Activity A to start C :
var intent: Intent =Intent(context, ActivityC::class.java)
startActivityForResult(intent,1009)
In Activity C setting result like this :
val returnIntent = Intent()
setResult(Activity.RESULT_OK, returnIntent)
finish()
The code is okey. But the thing is, that when you start activity b, then in your stack, activity b will be the last to be created, so when you then start c, the result from c goes to b instead of a.
Try launching c before b to make sure this is the issue.
If Activity B is from another SDK and you can't control the navigation flow, then you have to store the data somewhere and check that onResume() function of Activity A.
I have an application with 3 layers. A, B, C.
A and B are fixed and C can be any one of the multiple activities.
Application normally works in A>B>C direction.
A calls B with startActivityForResult method. B calls C with startActivityForResult too and result of C is processed in B and passed back to A via onActivityResult methods on all layers.
It works fine but I somehow want the activity A to call C directly (where i have implemented a thread to do stuff in background) using
Intent i = new Intent("com.intentntfiltername.ACVITIY-C");
startActivity(i);
C then switches back to A by resuming it.
After this A would call B for result.
B would then call C using
intentName.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivityForResult(intentName, requestCodeVariable);
that would not create new instance of Activity C but instead resume the old activity.
The problem is when I return to B after exiting C, onActivityResult is never called. It just resumes the B activity without processing any results from C.
i.e on Activity Result is not working when we try to call an already running activity for result.
Please tell me how to implement this. Or any other workaround only if its not possible for an already running activity.
when I finish an activity that was started for a result, I call for example
Intent intent = new Intent();
intent.putExtra("email", email);
act.setResult(RESULT_OK, intent);
act.finish();
where the returned value I needed is the email of course, and "act" is the activity that Ive created in the onCreate as
act = this;
This may sounds simple, but I am so bewildered by it. I have searched but not found any solution.
My question is: How to return to two activities when back button pressed?
Like this: let me say that I have activities A, B and C (A -> B -> C). What I want to achieve is when I am on activity C and press the back button, It should return me to activity A. When I am on B and press back, it should return me to A too.
It may be implemented into a project with many activities, so I assume that I don't need to set the class name of where to return, It should be recorded automatically by the android. How to achieve this?
Thank you
A possible solution is calling startActivityForResult() from Activity B, so that on the callback of the created Activity C, the previous Activity B gets finished as well. The callback function is called onActivityResult(), and in that, you want to call finish().
Example:
ActivityB:
Intent i = new Intent(this, ActivityC.class);
startActivityForResult(i, 0);
...
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
this.finish();
}
ActivityC:
//do stuff
This way, when you press Back (or call finish()) in ActivityC, it will call back on ActivityB's onActivityResult() function, and it will end them both.
You can override the onBackPressed method and sent an intent to the activity you want.
#Override
public void onBackPressed()
{
// code here to send intent to the activity A
}
One thing, I'm not sure if this goes well with the activity stack but you can alway try.
you can finish activity B , when you are starting intent for activity C, then activity stack will have activity A, and when you press back on activity C, activity A will be displayed.
just override onBackPressed in Activity C, and finish() it.
You can call the Activity that you want to go back to with a special flag (FLAG_ACTIVITY_CLEAR_TOP) from your onBackButtonPressed which will skip/remove the other activities in between. This way you can go back from C to A.
See: http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP
I think this is better than messing around with the finish() or starting activity for result when there is no result to return.
A --> B --> C
Intent intent = new Intent(this, A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
This will launch a new intent for Activity A and clears all the other activities from Stack.
how can i call an activity from stack instead of launching new instance ?
here is a scenario :
calling activity A with parameters in order to retrieve data
navigate from A to B
navigate from B to C
i want launch A again but not with a new instance , i want get it from the stack so no need to pass parameters and wait to retrieve data.
If I get your point correctly you can simple exit your activity B and C with finish();.
So if you ActivityC finishes and also ActivityB the ActivityA should come to the front, which should be that what you want.
Try to use FLAG_ACTIVITY_REORDER_TO_FRONT. For example:
Intent intent = new Intent(BActivity.this, AActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
From javadocs:
public static final int FLAG_ACTIVITY_REORDER_TO_FRONT
Added in API
level 3
If set in an Intent passed to Context.startActivity(), this flag will
cause the launched activity to be brought to the front of its task's
history stack if it is already running.
For example, consider a task consisting of four activities: A, B, C,
D. If D calls startActivity() with an Intent that resolves to the
component of activity B, then B will be brought to the front of the
history stack, with this resulting order: A, C, D, B. This flag will
be ignored if FLAG_ACTIVITY_CLEAR_TOP is also specified.
From ActivityA I'm starting ActivityB.
In ActivityB I'm creating a new Serializable object.
After the object has been created I want to close ActivityB and pass the new object to ActivityA.
How can I do it?
start Activity B with startActivityForResult().
In activity B, when the object is created create an Intent to pack the object in:
Intent result = new Intent();
result.putExtra("result", object);
setResult(RESULT_OK, result);
Then you will receive that intent in the onActivityResult() method of Activity A, where you can extract it like so:
data.getSerializableExtra("result");
Start the Activity B using startActivityForResult method.
When you finish creating object call setResult in Activity B. Set Your Data in Intent. You don't need to finish this.
Override function onActivityResult in Activity A. This will be called when you call setResult in Activity B. You can receive the data from Intent passed from Activity B.
But most of the time, you need separate Activities if only you have different screens with different tasks. Otherwise accomplish the task within the same Activity. *(A Good and Standard Practice).*
after the object is created, create an intent object, put that object to that intent and then start activity A. In the Activity A's onRestart() get that intent and from that intent get that object.
Why are you doing that? If you're using activity B only for creating new object, you can do it in a plain simple java class. What are you trying to accomplish?