0

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.

Georgi Yakov
  • 53
  • 3
  • 11
  • Take a look at this, https://stackoverflow.com/a/10407371/8551764 , `Activity.RESULT_OK` and `Activity.RESULT_CANCELED` would be what you are looking for. – Mostafa Arian Nejad Oct 23 '18 at 07:59

3 Answers3

1

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.

AIMIN PAN
  • 1,563
  • 1
  • 9
  • 13
  • 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`. – Georgi Yakov Oct 23 '18 at 08:16
  • 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? – AIMIN PAN Oct 23 '18 at 08:21
0

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.

Ümañg ßürmån
  • 9,695
  • 4
  • 24
  • 41
  • Is this the same as `SetResult(Result.Ok, intent)` and `SetResult(Result.Canceled, intent)`? – Georgi Yakov Oct 23 '18 at 08:21
  • 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. – Ümañg ßürmån Oct 23 '18 at 08:23
  • 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. – AIMIN PAN Oct 23 '18 at 08:25
  • but need to send some data to the main page and I can not do it without the intent. – Georgi Yakov Oct 23 '18 at 08:26
  • @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. – Ümañg ßürmån Oct 23 '18 at 08:28
0

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);
    }
}
Hadas
  • 906
  • 12
  • 22