How to manage multiple activity interactions in Android? - 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.

Related

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.

StartActivityResult and StartActivity for multiple Activities [duplicate]

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.

How to open intent and add value android

I have trouble with returning a value to another intent in android.
This is my situation:
I have Activity A, which is opened when I start my application.
When I click on a button I start Activity B.
In activity B I fill in a EditText and return a string.
Then I click on a button in activity B to return to activity A.
I don't know what method I should use in activity A to use the value I get returned from activity B.
this kind of situation fits good for startActivityForResult and onActivityForResult. When you press on the button, instead of calling startActivity you call startActivityForResult in order to start ActivityB. In ActivityB when you click on the button, you have to fill up an Intent with the values you want to return to ActivityA and, in order, call setResult(result, Intent) and finish(). The onActivityResult of ActivityA will receive the intent with your data
Activity Docs
You should start activity B by calling startActivityForResult() instead of startActivity() and on Activity B, in your exit button click listener, setResult() results to pass to Activity A.
You can handle the result by overriding onActivityResult() method in Activity A
Check this : http://developer.android.com/reference/android/app/activity.html
Search for "Starting Activities and Getting Results"

Intent flags on Android

I have a widget for my application, which need to be somewhat independent from the app.
The activity workflow should be like this:
Widget -> Activity acting as receiver
Receiver -> LoginPage or Activity A (depending on login status)
LoginPage -> Activity A
Activity A onKeyDown -> Activity B
Activity B onKeyDown -> Home Screen.
I have no problem until Activity B, which sends back to Activity A when I press onKeyDown. I'm using FLAG_ACTIVITY_CLEAR_TOP flag and finishing the Activity when starting the activity B.
When I move from ActivityA to ActivityB using the CLEAR_TOP flag, I supposed that Activity stack is cleared, then in ActivityB I finish the Activity on the onKeyDown() method, assuming that the App will be closed, but it doesnt. Why?
I'm also trying to use FLAG_ACTIVITY_CLEAR_TASK and FLAG_ACTIVITY_NEW_TASK in the receiver but I dont understand the mechanism pretty much. Any idea about this?
In fact the FLAG_ACTIVITY_CLEAR_TOP, start your activity B if its not started or it came back as the second activity on the BackStack. To finish Activity A, you can call finish() after starting Activity B or add no history flag, when starting A.
#JesusS: I doubt if u can finish ur activity in that fashion during a forward transition.
Consider a scenario of moving from Activity A to Activity B. Now if u want to kill Activity A and want to move to Activity B then call the startActivity(intent);
(where ur moving from activity A to B)
without any flags on the intent followed by the finish() on activity A.
As per my understanding u can use Intent.FLAG_ACTIVITY_CLEAR_TOP only during backward transition i.e when u already have that activity on the stack.
Consider the following scenario:
A --> B --> C --> D
Now if u want to move back from activity D to Activity A by clearing the activities u can go for Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP.
The result is that the Activities D, C, B(LIFO) will be removed from the stack and the activity A resumes by calling the onResume() of Activity 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