can i wait for two different results with StartActivityForResult()

android startactivityforresult return result
startactivityforresult multiple activities
startactivityforresult example
onactivityresult not called
setresult android
startactivityforresult from fragment
flag_activity_forward_result
onactivityresult overrides nothing

Hello I want to ask if it is possible to wait for 2 different results with StartActivityForResult(). What I am trying to accomplish is the following:

I have a ListView and when I click an item I start another activity. There I have 2 Buttons. One of them is to save changes of the name of list item and the other one is to delete the clicked item. I am not sure exactly how to detect which of the buttons I have pressed.

Yes. The second parameter REQUEST_ID is to be used to tell which request you are asking form.

When button 1 clicked, you call

startActivityForResult(intent, REQUEST_1)

when button 2 clicked, you call

startActivityForResult(intent, REQUEST_2)

and in the callback, you have it as the first parameter:

onActivityResult(requestCode: Int...)

if requestCode == REQUEST_1, it is from button 1.

Getting a result from an activity, You can also start another activity and receive a result back. While the underlying startActivityForResult() and onActivityResult() APIs are available If you have multiple activity result calls that either use different contracts or  second_main.xml. This xml file is created automatically when you create another activity. To create new activity Right click on the package inside the src -> New -> Other ->Android Activity. Now drag one editText, one textView and one button from the pallete, now the xml file will look like this: File: second_main.xml.

Solution:

First, start you activity with startActivityForResult(your_intent, 1);

Second, in your next activity (where you have 2 buttons):

In your save button's click listener, pass resultcode as "1" as shown:

setResult(1);
finish();

In your delete button's click listener, pass resultcode as "2" as shown:

setResult(2);
finish();

Finally, in onActivityResult() of your previous activity/fragment:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 1 && resultCode == 1) {
        ....
        (Save Button Pressed)
        ....
    }
    else if (requestCode == 1 && resultCode == 2) {
        ....
        (Delete Button Pressed)
        ....
    }
}

That's it. Working example from my own project.

Android Programming: The Big Nerd Ranch Guide, AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) Setting a result There are two functions you can call in the child activity to send data back (You can use another constant, RESULT_FIRST_USER, as an offset when to the parent if the child activity was started with startActivityForResult(. If you need to do that several times, that's fine too: that's why startActivityForResult() takes an int requestCode argument, which gets passed back to onActivityResult(), so you know which request is returning.

To make it more clear I'll call the first activity (the one with the list) - ListActivity, and the new activity (the one with the buttons) - ButtonActivity.

To detect the button that was pressed in ButtonActivity add an implement to the class like this:

public class ButtonActivity extends AppCompatActivity implements View.OnClickListener

Then implement the method onClick(View v), like this :

@Override
public void onClick(View v) {
    int id = v.getId();
    switch (id) {
        case R.id.your_save_button_id: {
            Intent data = new Intent();
            data.putExtra("item_to_save", item);
            setResult(CommonStatusCodes.SUCCESS, data);
            finish();
            break;
        }

        case R.id.your_delete_button_id: {
            Intent data = new Intent();
            data.putExtra("item_to_delete", item);
            setResult(CommonStatusCodes.SUCCESS, data);
            finish();
            break;
        }
        default: {
            break;
        }
    }

}

In your ListActivity:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == "request_code_for_button_activity") {
        if (resultCode == CommonStatusCodes.SUCCESS) {
            if (data != null) {
                if (data.getStringExtra("item_to_delete") != null) {
                    //DELETE YOUR ITEM HERE
                } else if (data.getStringExtra("item_to_save") != null) {
                    //SAVE YOUR ITEM HERE
                }
            }
        }
    } else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}

Android, It does not matter what you send, so here you pass 0 for the required parameter. Setting a result There are two methods you can call in the child activity to send  StartActivityForResult is used to receive result back from android activity like if your starting camera application into your mobile phone then as the final result you want to display your captured photo on mobile phone screen. So here is the complete step by step tutorial for Android StartActivityForResult example tutorial.

Streamlining intents with Kotlin coroutines - David Air, Let's say you have 2 activities and you want to send back an If no activity was killed, it was a matter of just chaining setResult() and onActivityResult(). NOTE 1: if your parent activity will be finished as well, you can use this flag instead of startActivityForResult (since it won't handle any result as the flag  SubName: StartActivityForResult Description: This code demonstrates how JavaObject can be used to call external APIs that should be called with Context.startActivityForResult. This code should be added to an Activity. You should also declare a process global variable named ion: Sub

startActivityForResult, setResult & onActivityResult, startActivityForResult(intent, RC_GET_IMAGE) }override If your activity does not extend AppCompatActivity(), you'll need to create a copy of  Architecture behind Fragment's startActivityForResult. Although we could call startActivityForResult directly from Fragment but actually mechanic behind are all handled by Activity. Once you call startActivityForResult from a Fragment, requestCode will be changed to attach Fragment's identity to the code. That will let Activity be able to track back that who send this request once result is received.

Xamarin Doctor, Hello I want to ask if it is possible to wait for 2 different results with StartActivityForResult(). What I am trying to accomplish is the following: I have… Hello I want to  My results have also varied wildly, depending on the test, from a low of 101 (I forget which test) to a high of 154 on the Mensa. I just now took the European test online so I could compare for the purposes of this question and got this result: Y

Comments
  • Take a look at this, stackoverflow.com/a/10407371/8551764 , Activity.RESULT_OK and Activity.RESULT_CANCELED would be what you are looking for.
  • the problem is that I call StartActivityForResult() from the main page with a chosen request number. And when I choose one of the to buttons and set the result it will go to the same requestCode.
  • the request code from callback is just for you to tell which request if you have many request, and should not be used elsewhere. startActivityForRequest ( intent, R.id.button1) and startActivityForRequest ( intent, R.id.button2), and in onActivityResult, check : if (requestCode == R.id.button1) ... else ... is this clear?
  • Is this the same as SetResult(Result.Ok, intent) and SetResult(Result.Canceled, intent)?
  • Yes, But there is no intent used in this solution. This setResult(..) is an Activity Method. @GeorgiYakov By far, this was the easiest of all the solutions I saw.
  • Yes this works if you the opening activity is your own. But if you open choose image activity, which you can't set result code.
  • but need to send some data to the main page and I can not do it without the intent.
  • @GeorgiYakov Sorry, I was mistaken.. It seems that you can write intent.. SetResult(Result.Ok, intent) is correct with this solution too.. use data in onActivityResult() to get the result from previous buttons.