StartActivityResult and StartActivity for multiple Activities [duplicate] - android

This question already has answers here:
Passing data though multiple activities
(2 answers)
Closed 6 years ago.
I have three Activities.
Activity A: startActivityResult calling Activity B and getback to A
with Data
Activity B: only startActivity to Activity C and send Data to
Activity C.
Activity C: Now here i want to getData from B send to A with
startActivityResult.
How to go Activity C to A with data with
setResult(); finish();
method?

Just as you go from ActivityB to ActivityC giving data, use the same method (start intent with Bundle I suppose) to go from ActivityC to ActivityA.
In ActivityA, check if there's Bundle, and react if there's something.

If from Activity C you want to close Activity B & C and go to A then you have two choices.
startActivity A from C with Intent.FLAG_ACTIVITY_CLEAR_TOP this will call onNewIntent method from activity A and will remove all activities that are on top of activity A.
Change the implementation to have:
a. From activity B startActivityForResult(Activity C).
b. In activity C, when it's done call setResult/finish
c. In activity B, in onActivityResult check the request code, if is C request code, setResult/finish
d. Activity A it's visible again with result.
If you don't want to close Activity B & C just start the activity a and pass data to it as you start activity B.

Related

startActivityForResult usage for 3 activity Android

Hey guys I have 3 activity,
Activity A -> Activity B -> Activity C
This flow I want to perform in my application. I want this scenario in my Activity C
Scenario 1
On Back press from Activity C to open Activity B then again back press then open Activity A
Scenario 2
When something submit on Activity C, I want to open Activity A and I want to hide Activity B
So how can I do this through startActivityForResult. I check through this stackoverlow, but nothing works. Thanks
I'm assuming that the 3 activities are opened in order A -> B -> C, so they are in the backstack in that order. You can check the return values from Activity C, and act accordingly.
In Activity C, setResult(Activity.RESULT_CANCELLED) during onCreate(). That will make it the default return value when activity C is closed (back press or otherwise)`.
Then setResult(Activity.RESULT_OK) whenever something is submitted. After that, you can fininsh() that activity.
Whenever Activity C get closed, in Activity B onActivityResult(), check the return value. If it's RESULT_OK, call finish() (which will return to Activity A). Otherwise, do nothing, so Activity B will stay open

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

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.

How to get data from the second intent call to one Activity [duplicate]

This question already has answers here:
How do I pass data between Activities in Android application?
(53 answers)
Closed 5 years ago.
I'm building a small quiz app with 3 activities A, B, C.
A startActivity() to B.
B has a fragment B_frag to show the quiz detail, after finish the quiz B_frag will startActivity() to C which is the result activity.
C has an option to redo the quiz which will startActivity() to B again, this time with a string through intent.putExtra().
The problem is nothing comes out of the intent.getStringExtras() on activity B after being called from C.
Can someone pls tell me why this happens and how to fix it?
From the Activity C try to use FLAG_ACTIVITY_SINGLE_TOP when you call startActivity() to start Activity B. Then in Activity B override onNewIntent() to get the new intent from Activity C.
Show us some code.
For sending data from Activity A to Activity B, try this:
intent.putExtra("someKey", yourView.getText().toString());
In Activity B:
Bundle bundle = getIntent().getExtras();
if (bundle!=null){
yourString= bundle.getString("someKey", "no name");
}
yourView.setText(yourString);
Try setting flag new task when calling startactivity from c to b.

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.

Android activities question

I have three activities, A, B and C.
User can start activity B from A, and then C from B.
I would like to have following. In C the user fill some field and click "OK"-Button. After that both activities B and C should be finished.
I don't like my idea, to finish A after starting B, then finish B after starting C, and then start A from C.
What is the best way to do that?!
Is it possible to finish parent activity from the child? If, yes, then that would be my solution. But i haven't find any information about that.
Thank you,
Mur
in Activity B, call Activity C with startActivityForResult. In the ActivityB.onActivityResult() method, call finish() when Activity C returned with a good result. This gets you back to Activity A.
try This
In Activity C Class
Intent mIntent=new Intent(getApplicationContext(),A.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivityForResult(mIntent);

Categories

Resources