Can Poyrazoğlu
Can Poyrazoğlu

Reputation: 34780

startActivityForResult crashes while startActivity works

I have an activity that I'm starting:

Intent intent = new Intent(this, CreateItemDetailsActivity.class);
if(storeId != null) {
    intent.putExtra(Identifiers.STORE_ID, storeId);
}
intent.putExtra(Identifiers.ITEM_NAME, name);
intent.putExtra(Identifiers.ITEM_DESCRIPTION, description);
startActivity(intent);

This works, but now I need to return data to the original activity, so I change startActivity call to:

startActivityForResult(intent, CREATE_ITEM_RESULT);

(CREATE_ITEM_RESULT is just a random integer number 63463657 I made up)

However, my app crashes without neither onCreate methods (I've implemented and put a breakpoint in both) being called:

Uncaught exception in thread main: java.lang.IllegalStateException: Could not execute method for android:onClick
android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:389)

(the code is called in a button click handler, hence onClick)

I've seen App crashes when calling startActivityForResult which has the same problem but the accepted answer suggests removing a line, recipe.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);, which I don't have.

Why am I getting a crash on startActivityForResult while startActivity works perfectly?

(My min SDK level is 16, target and compile SDK levels are 27, and I'm running Android 8.1 emulator)

Upvotes: 0

Views: 1114

Answers (2)

MidasLefko
MidasLefko

Reputation: 4549

In the non-support Activity class implementation of startActivityForResult there is no restriction on the maximum int for requestCode, as long as it is positive. However FragmentActivity (and by extension AppCompatActivity) overrides this behavior (documentation):

void startActivityForResult (Intent intent, int requestCode)

Modifies the standard behavior to allow results to be delivered to fragments. This imposes a restriction that requestCode be <= 0xffff.

In the source code of FragmentActivity one finds the following helpful comment:

A hint for the next candidate request index. Request indicies are ints between 0 and 2^16-1 which are encoded into the upper 16 bits of the requestCode for Fragment.startActivityForResult(...) calls. This allows us to dispatch onActivityResult(...) to the appropriate Fragment. Request indicies are allocated by allocateRequestIndex(...).

ints in java are 32 bit, FragmentActivity uses 16 to determine which fragment to send the result to and the other 16 are available for the developer.

Upvotes: 2

Barns
Barns

Reputation: 4848

If have not seen any documentation on this issue, but try a four digit int value should get rid of the error.

I have read Google docs for startActivityForResult and I have not found any reference to a maximum int size. Even the code documentation states:

@param requestCode If >= 0, this code will be returned in the onActivityResult() when the activity exists

So, no mention of a maximum int value. Odd...


EDIT

Thanks to @MidasLefko for finding this information:

RequestCodes can only be a max of 0xffff (65535). So you are probably calling startActivityForResult(intent, REQUEST_CODE); and REQUEST_CODE is greater than 65535.

on this website:

https://code-examples.net/en/q/db5947

Upvotes: 4

Related Questions