Unable to get result back to onActivityResult() of calling activity - android

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.

Related

Getting a Result from an Activity

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

How to manage multiple activity interactions in Android?

In my activity, i'm calling activity B from activity A (in back stack A->B), than again activity C from B (in back stack A-> B-> C) for some result to reflect on activity A, than how to jump to activity A on button click with carrying some result from activity C to reflect back in activity A(while clearing back stack of activity B,C)
Option 1: You can chain calls of startActivityForResult() and "unwind" the chain in the onActivityResult() callbacks.
Activity A calls startActivityForResult() to start Activity B
Activity B calls startActivityForResult() to start Activity C
Call setResult(RESULT_OK) and then finish() in Activity C
Result of Activity C is received in onActivityResult() of Activity B. Process the result (if needed).
Call setResult(RESULT_OK) and then finish() in Activity B
Result of Activity B is received in onActivityResult() of Activity A. Process the result.
Option 2: Start Activity A again, but add flags to the intent.
Intent intent = new Intent(this, ActivityA.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// add more data to Intent
startActivity(intent);
finish();
This intent will be received in the onNewIntent() method of Activity A.
EDIT
Option 3: Use LocalBroadcastManager to send broadcasts locally (within your process only). This requires registering a BroadcastReceiver dynamically in Activity A, then sending the broadcast from Activity C. You will still need to use one of the two techniques above to get back to Activity A and clear the stack; this just changes how the data is transmitted back to Activity A.

OnActivityResult not called for an already running activity

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;

Android: How to pass back key to the activity behind

All:
Is there a way that can pass back key to the activity behind?(Two activities A,B; B is on top of A; How to let B ignore back key and let A receive back key?)
Thanks a lot!
BR.
use StartActivityForResult(Intent, int) if you start Activity B from Activity A.
Then in B: set the result on the Intent through getIntent() and finish() activity B
then override onActivityResult in Activity A to get the value
start activity B with following flags added to that intent.
intent.setflags(INTENT.FLAG_ACTIVITY_NO_HISTORY)
startActivity(intent);
then your back will be the back from your previous activity.
Probably you might exit the app if there are no activities behind A
or another simple solution is,
call finish() after you call startActivity(...) for B in A.

Android finish parent Activity

I have 3 different activities in a TabGroupActivity. Let's say A - Parent, B - Child 1, C - Child 2.
A --- starts --> B
B --- starts --> C
and I have an alert dialog in C which shows some message. I want to go back to activity A when I press Ok button on dialog.
But the problem is that I can open activity C from other activities too and I want to go back to their parent activities too. So I need to make something which will work no matter which activity opens C. I've tries with this one but didn't work :
Intent intent = new Intent(Synchronization.this,Synchronization.this.getParent().getClass());
but it didn't help me. Any suggestions?
You just have to make use of two Activity methods viz. startActivityForResult() and onActivityResult()
Example : http://www.vogella.de/articles/AndroidIntent/article.html#explicitintents
Here goes the logic :
In ActivityB
Start ActivityC by using startActivityForResult(activityCIntent,INT_CODE);
In ActivityC
Now check if Dialog's OK Button is pressed, if yes then set the result using setResult(RESULT_OK,intent); and then call finish();
Then control will be redirected to ActivityB's onActivityResult() method.
Now inside onActivityMethod() check whether result_code==RESULT_OK and requestCode = INT_CODE. If yes then simply call finish();
In the Activity B start the C Activity as startActivityForResult() so when you finish the C activity it will back to the B with result. As the result you may pass flag with the intent object.
Now when you finish the C activity with ok button then set the result as RESULT_OK into the setResult() if you need to pass the data back to the B activity you may set the data into the Intent add this intent with the setResult() method and then finish the C Activity.
Now in B check the requestcode is from the C then finish this Activity. As you start this C Activity you can also start the B Activity for the A Activity.
And you need to override onActivityResult() in B activity and if you start the B Activity as for result then also you need to define also into A Activity

Categories

Resources