This question already has answers here:
Sending data back to the Main Activity in Android
(13 answers)
Closed 8 years ago.
I have 2 activities: first activity whith Button, which calls The second activity (Open File Dialog) by intent. How to return filename from the second activity to first? By intents ? Which way is worth using ? It`s requiredd to use api <=15.
Refer this answer here:
To start another activity, only to get a response from the activity as in your case, you can use: startActivityForResult(Intent intent, int identifier_value);. In your second activity, you can choose to return a result or cancel the result.
Docs states:
The startActivity(Intent) method is used to start a new activity, which will be placed
at the top of the activity stack. It takes a single argument, an Intent, which describes
the activity to be executed.
Sometimes you want to get a result back from an activity when it ends. For example,
you may start an activity that lets the user pick a person in a list of contacts; when it
ends, it returns the person that was selected. To do this, you call the
startActivityForResult(Intent, int) version with a second integer parameter identifying
the call. The result will come back through your onActivityResult(int, int, Intent) method.
When an activity exits, it can call setResult(int) to return data back to its parent.
It must always supply a result code, which can be the standard results RESULT_CANCELED,
RESULT_OK, or any custom values starting at RESULT_FIRST_USER. In addition, it can
optionally return back an Intent containing any additional data it wants. All of this
information appears back on the parent's Activity.onActivityResult(), along with the
integer identifier it originally supplied.
Related
I use startActivityForResult with an intent and a specific REQUEST_CODE to launch activity B from A.
In activity B the user can edit the database entry, or delete it.
If I press the same button, and launch the activity using the same request code and intent,
how can I check if the user chose to save the entry they edited, or delete it?
As stated in the comments, I'd personally use different REQUEST_CODEs for different operations, but if you truly want to respond with the same one, and indicate something, simply pass a boolean (or any other useful type) in the response's intent that you can handle in the onActivityResult in the calling activity or fragment.
I have four activities, say A, B, C and D.
My situation is A will start the activity B by startActivityForResult.
startActivityForResult(new Intent(this,B.class),ONE);
In another situation I will start activity B with a different request code, like:
startActivityForResult(new Intent(this,B.class),TWO);
In B, I need to call C or D depending on requestCode. I.e if ONE need to start C else D.
So I need to know how to check the requestCode in the child Activity (B here).
In other words, I want to get the request code that Activity B was started with.
You can pass request code by put extra.
intent.putExtra("requestCode", requestCode);
Or if you have used startActivityForResult many times, then better than editing each, you can override the startActivityForResult in your Activity, add you code there like this
#Override
public void startActivityForResult(Intent intent, int requestCode) {
intent.putExtra("requestCode", requestCode);
super.startActivityForResult(intent, requestCode);
}
So there is no need to edit all your startActivityForResult
Hope it helped you
The request code is not passed to the started activity automatically because it doesn't (and shouldn't) need to know this value. It only needs to know what to do and not where it was started from.
Starting an activity is really just another form of calling a method. When you call a method, you receive the result synchronously (right there where you made the call). In this case you are only passing in the information that method needs to do its work. You are not telling it where you called it from.
Starting an activity is the asynchronous analog of calling a method, in which case you receive the result in the special method onActivityResult(). In this method, you need to know what to do with the result you just received and you have the request code for this.
To make it a bit clearer why it isn't a good idea to pass the request code as a parameter, consider the example activity which is showing a product you can buy. On this activity there are two buttons labeled "Buy" and "Login" (as you are currently not logged in). Pressing "Login" will start an activity named "Login" which will try to log in the user using the provided information. Pressing "Buy" will first start the very same "Login" activity and if the login was successful, start the buy activity.
Now, the "Login" button uses request code 1 to start the login activity, but the "Buy" button can't use the same request code as it will have to do something different if the login is successful. So, the "Buy" button uses request code 2.
In the "Login" activity you might receive two different request codes depending on where it was called from, but you will need to do the very same procedure.
So, if you pass in the request code as a parameter, you will end up with code that needs to do the same stuff for a couple of different request codes, like:
if (requestCode == LOGIN || requestCode == BUY) {
// ...
} else ...
You will also end up with storing the request code constants in a central location e.g. a class named RequestCodes.
In short, the request code should only be used to decide what to do with the received result. This way you will end up with a more modular, easier to maintain and easier to extend code.
I ended up using custom Intent action to pass this kind of information to the launching Activity.
protected static final String ACTION_DO_C = "do_c";
protected static final String ACTION_DO_D = "do_d";
Then you'd go like:
final Intent intent = new Intent(this,B.class)
intent.setAction(ACTION_DO_C);
startActivityForResult(intent,ONE);
And in Activity B you get the action easily:
getIntent().getAction();
You can use getCallingActivity() to get the activity that started current activity and that will receive the result value with response code at the end.
I have 2 Activities: DetailsBuildingActivity and EditBuildingActivity. DetailsActivity has a button to go to the EditBuildingActivity, to edit properties of the building (like name, size, etc). After editing and saving, you the DetailsBuildingActivity opens up with the new adjusted data. Doing this a few times back and forth in 1 go, gives me the following problem:
When doing 5 times in a row, but then I press the backbutton, I have to press it 10 times before I can get to my previous Activity. (10 times - 5 times DetailsBuildingActivity and 5 times EditBuildingActivity). Is there a way to solve this? I constantly make an intent to open each other op.
You need to use the activity stack.
When you save in the EditBuildingActivity you need to call finish() and not create a new intent.
If you would like DetailsBuildingActivity to know about EditBuildingActivities status, you can use
startActivityForResult(intent, requestCode) to start the activity.
setResult() && finish() to complete the launched activity
and
--- ANDROID DOCS
protected void onActivityResult (int requestCode, int resultCode, Intent data)
Since: API Level 1 Called when an activity you launched exits, giving you the requestCode you started it with, the resultCode it returned, and any additional data from it. The resultCode will be RESULT_CANCELED if the activity explicitly returned that, didn't return any result, or crashed during its operation. You will receive this call immediately before onResume() when your activity is re-starting.
Remember that activities can have multiple instances, and every time you start a new one with a intent they are typically added to the top of your activity stack.
I have four activities, say A, B, C and D.
My situation is A will start the activity B by startActivityForResult.
startActivityForResult(new Intent(this,B.class),ONE);
In another situation I will start activity B with a different request code, like:
startActivityForResult(new Intent(this,B.class),TWO);
In B, I need to call C or D depending on requestCode. I.e if ONE need to start C else D.
So I need to know how to check the requestCode in the child Activity (B here).
In other words, I want to get the request code that Activity B was started with.
You can pass request code by put extra.
intent.putExtra("requestCode", requestCode);
Or if you have used startActivityForResult many times, then better than editing each, you can override the startActivityForResult in your Activity, add you code there like this
#Override
public void startActivityForResult(Intent intent, int requestCode) {
intent.putExtra("requestCode", requestCode);
super.startActivityForResult(intent, requestCode);
}
So there is no need to edit all your startActivityForResult
Hope it helped you
The request code is not passed to the started activity automatically because it doesn't (and shouldn't) need to know this value. It only needs to know what to do and not where it was started from.
Starting an activity is really just another form of calling a method. When you call a method, you receive the result synchronously (right there where you made the call). In this case you are only passing in the information that method needs to do its work. You are not telling it where you called it from.
Starting an activity is the asynchronous analog of calling a method, in which case you receive the result in the special method onActivityResult(). In this method, you need to know what to do with the result you just received and you have the request code for this.
To make it a bit clearer why it isn't a good idea to pass the request code as a parameter, consider the example activity which is showing a product you can buy. On this activity there are two buttons labeled "Buy" and "Login" (as you are currently not logged in). Pressing "Login" will start an activity named "Login" which will try to log in the user using the provided information. Pressing "Buy" will first start the very same "Login" activity and if the login was successful, start the buy activity.
Now, the "Login" button uses request code 1 to start the login activity, but the "Buy" button can't use the same request code as it will have to do something different if the login is successful. So, the "Buy" button uses request code 2.
In the "Login" activity you might receive two different request codes depending on where it was called from, but you will need to do the very same procedure.
So, if you pass in the request code as a parameter, you will end up with code that needs to do the same stuff for a couple of different request codes, like:
if (requestCode == LOGIN || requestCode == BUY) {
// ...
} else ...
You will also end up with storing the request code constants in a central location e.g. a class named RequestCodes.
In short, the request code should only be used to decide what to do with the received result. This way you will end up with a more modular, easier to maintain and easier to extend code.
I ended up using custom Intent action to pass this kind of information to the launching Activity.
protected static final String ACTION_DO_C = "do_c";
protected static final String ACTION_DO_D = "do_d";
Then you'd go like:
final Intent intent = new Intent(this,B.class)
intent.setAction(ACTION_DO_C);
startActivityForResult(intent,ONE);
And in Activity B you get the action easily:
getIntent().getAction();
You can use getCallingActivity() to get the activity that started current activity and that will receive the result value with response code at the end.
I have a question on the Android Activity, for example I have a TabHost, and there are included four Activities, the first tab is a search Activity, enter a keyword in the current result of this Activity to return, and in the current Activity display. Is called to display the search results themselves. And after searching several times, and then return to key mobile phone keypad, the display is the result of the last search keyword, I want the press back key to return to the last call of the Activity or TabHost. Should I do?
By the way, in a tab in the use of Intent calls a Activity,
eg: host.addTab (host.newTabSpec ("friend"). setIndicator ("search")
. SetContent (new Intent (this, Search.class)));
In this Activity in the need to call another Activity,
e.g: startActivity (new Intent (this, Other.class));
Also called another Activity displayed on this tab, but not yet jump out of the show. I ask how you can achieve this?
First, you can use startActivityForResult(...) instead of startActivity(...). This means that when you're done with the activity, you want to come back to the activity that started it so you can do more.
Second, you can override the onKeyDown(...) method and define whatever behavior you want for the back key. however, this is not recommended by Google except when absolutely necessary.