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
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 several Activities in Android A,B,C,D. I have a question about the back stack.
A->B->C->D it is a normal sequence.
My question is : when i press the back button in Activity D, i have to go to the Activity B, not back to ActivityC. Since ActivityC have some imageView i want to save, i don't want to use noHistory to destroy the ActivityC. it is possible to do that
A1->B1->C1->D1
the back stack should be: D1->B1->A1 . C1 saved some imageView and if C1 launch again, the imageView in C1 will contain the same images. Is it possible just modify the code in ActivityC??
thank you
You should call finish() in activity C after, starting of activity D
startActivity(intent);
finish()
Handle onBackPressed() of each activity. So that after back pressing on D, it will redirect to B, then similarly to A.
Use onActivityResult() methods of an activity:-
Here is how it will work -
Use startActivityForResult() method while you are starting activity
While when you are going back
Activity D - before finishing write -
Intent i = getIntent();
setResult(RESULT_OK);
finish();
Your D is finished and c's onActivityResult() will be called - save your data and call finish() with above methodology, It will go to B's onActivityResult().
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.
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"
I have 4 Activities A,B,C,D now I have in each activity a button of Back and button Cancel
Now I went on the button back to move to the activity that I have before example if I am on B when I press on back I get to A that I have done that by finish but the problem that I have is when I am on example C and I press cancel I should get to A but dose not work I try to make a new activity but don't work.
how can I make my function Cancel to take me back to the activity A?
Just use onActivityResult() in middle activity, and while finishing C just add the line
setResult(5)
in the middle Activity B in onActivityResult
just check in resultcode=5 finish it.
check this link
You can use Intent with flag FLAG_ACTIVITY_CLEAR_TOP in startActivity.
So if you have A B C after launching A activity B and C will be closed.
For example on Activity C in cancel button click listener:
Intent cancelIntent = new Intent(this, A.class);
cancelIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(cncelIntent);