Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

startActivityForResult(Intent intent, int requestCode, Bundle options) how to retrieve the extra bundle option?

According to the Android documentation public void startActivityForResult (Intent intent, int requestCode, Bundle options).

I can not figure it out how to retrieve the extra bundle options that I am passing on the intent.

I want to pass an ArrayList with data as an extra bundle option when I am calling the startActivityForResult method.

Sample of code:

ArrayList<String> list = new ArrayList<>();
        list.add("test1");
        list.add("test2");

        Bundle bundleOptions = new Bundle();
        bundleOptions.putStringArrayList("key", list);

startActivityForResult(intent, 10, bundleOptions);

Upon retrieving the data:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)

Bundle extras = data.getExtras();

Bundle extras does not contain the extra bundle that I am trying to pass. What I am missing and I can not retrieve the extra Bundle data that I am passing to the method?

I also tried with intent.putExtra("key", bundleOptions);, also with intent.putExtra("key", list); but with no success either and calling the method startActivityForResult(intent, 10); but again with no success.

I am sure that I am missing something does anybody know how to achieve this?

like image 335
Thanos Avatar asked Feb 15 '16 18:02

Thanos


1 Answers

I am sure that I am missing something

I suspect you misunderstand how the result thing works.

Let's suppose you have a HomeActivity and a SettingsActivity. HomeActivity starts a SettingsActivity with some parameters and wants to know some result. Here's how it works:

HomeActivity

This is how you open the SettingsActivity:

public void openSettings() {
    Intent i = new Intent(this, SettingsActivity.class);
    i.putExtra("myParam", 1);
    startActivityForResult(i, 10);
}

This is the call you receive when SettingsActivity is closed:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 10) {
        if (resultCode == RESULT_OK) {
            // Get result from the result intent.
            String result = data.getStringExtra("myResult");

            // Do something with result...
        }
    }
}

SettingsActivity

This is just the necessary bit. Reads the input, builds the output and closes itself. I hope it's enough for a demonstration.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Retrieve the parameter.
    int param = getIntent().getIntExtra("myParam");

    // Get a result somewhere.
    String resultValue = "RESULT=" + param;

    // Build a result intent and post it back.
    Intent resultIntent = new Intent();
    resultIntent.putExtra("myResult", resultValue);
    setResult(RESULT_OK, resultIntent);
    finish();
}

See this http://developer.android.com/training/basics/intents/result.html.

like image 149
Eugen Pechanec Avatar answered Sep 28 '22 04:09

Eugen Pechanec