I am switching between activities (1 and 2) via Up Navigation:
Activity 1 delivers data via Intent.putExtra to activity 2.
In activity 2 this data has to be delivered back to activity 1 when using its Up Navigation.
How do I have to code this?
You should start activity 2 with startActivityForResult, that way it can send back data to the starter activity.
For more info check out this tutorial on the Android developer site.
You have to start Activity2 with startActivityForResult(Intent intent, int requestCode) method.
On Activity2 you have to set the data you want to return with the setResult(int resultCode, Intent data) method
On Activity1 you have to implement the onActivityResult(int requestCode, int resultCode, Intent data) method. This is where you get the data from Activity2
Look the Activity doc for more information enter link description here
Related
I launch an Activity B from Activity A with startActivityForResult() and Activity B is translucent then Activity's A method
protected void onActivityResult(int requestCode, int resultCode, Intent data) {}
works immediately even Activity B is open.
Please suggest.
I just had the exact same problem. I solved it by removing the Intent.FLAG_ACTIVITY_NEW_TASK flag from the intent used to call Activity B. You could also check if there is
singleInstance
or
singletop
in your manifest in Activity B.
"You can't use startActivityForResult() if your activity is being launched as a singleInstance or singleTop."
Source : Android - startActivityForResult immediately triggering onActivityResult
I wish to know if the user has pressed back in card.io scan to react accordingly in my activity. Can I do that ? Does card.io send an intent?
The calling activity's onActivityResult(int requestCode, int resultCode, Intent data) method, resultCode will have a value of Activity.RESULT_CANCELED.
The 'Sample Code' section of the README has more detail. Also, check out the sample app.
I have 5 intents in one activity. All of them are using the startActivityForResult. Because all of them are to correspond to different elements in the layout. How do I recognize which intent is the result for.
For e.g. If I have intent1, intent2, intent3 all of which are using startActivityForResult. After the Intent business is done. How do I now in my onActivityResult recognize which intent was called. The intent being called upon is the android phone contacts intent.
When you call startActivityForResult(), you set the requestCode. Later, you can use this request code inonActivityResult() to determine the intent. see What is Intent from onActivityResult Parameters for more. If you see the documentation of onActivityResult, it mentions:
protected void onActivityResult (int requestCode, int resultCode, Intent data)
requestCode: The integer request code originally supplied to startActivityForResult(), allowing you to identify who this result came from.
Assume that the application has two activities, namely Activity1 and Activity2.
Activity1 is responsible to load some bunch of text and audio files. During the loading process Activity1 disposes progress dialog. After successfully loading, then comes the Activity2.
In my application Activity1 must run only once. If the user presses back button on Activity2,then application must terminate. But what I have seen that, Activity1 comes to the screen if the back button is pressed.
How can I achieve this? Is there any way to terminate application in the case of user presses back button on Activity2?
Any help will be appreciated.
Thanks
You could just have finish() after you start intent for Activity2.
Intent intent = new Intent(this, Activity2.class);
startActivity(intent);
finish();
Your could override onActivityResult in Activty1 which will be called when Activity2 exits and returns control to it.
Then something like:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
finish();
}
Should close activity1 after activity2 is closed.
I've got Activity A which fires up the Camera intent via:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CAMERA);
After the picture is taken I can easily grab the picture in:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
But I'd like to receive the result in Activity B in which the image can be edited.
Right now I'm receiving the result in Activity A and pass it over to Activity B which results in showing the GUI of Activity A for a short while:
Intent i = new Intent().setAction("DisplayJPEG");
i.setClass(this, EditImageActivity.class);
i.putExtra("IMAGE_URI", uri);
startActivityForResult(i, REQUEST_EDIT_IMAGE);
Of course, I will need the result from Activity B in Activity A after the image has been edited. But that should work with:
setResult(resultCode, data);
So there has to be a way to do what I need. Please point me into the right direction.
Have you tried launching ActivityB, and in ActivityB onCreate event launch the Camera Intent?
You technically can't do what you're asking. You'll need to find a way to continue passing it the way you are and hide the UI or do as Pentium says and do it the other way around.
Edit: Nevermind, I misread how this works. What actually happens is you can use Activity A to start Activity B for result, but then if Activity B needs to start Activity C to continue processing whatever Activity A wanted, you can use FLAG_ACTIVITY_FORWARD_RESULT to make Activity C return its result to Activity A not B.
I haven't looked into this more than a quick glance, but I noticed an Intent flag called FLAG_ACTIVITY_FORWARD_RESULT which according to the documentation:
If set and this intent is being used to launch a new activity from an existing one, then the reply target of the existing activity will be transfered to the new activity. This way the new activity can call setResult(int) and have that result sent back to the reply target of the original activity.
Like I said, I haven't experimented with this, but that seems to suggest that you could launch your camera intent from Activity A but have it forward its result to Activity B.