I'm trying to pass data from one activity to another however the onActivityResult is not being triggered. With my startup activity which is called MainActivity, I'm able to view an image gallery which will then trigger the onActivityResult with this piece of code.
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
From another Acitivity I have this piece of code
gridLayout.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent =new Intent();
setResult(RESULT_OK,intent);
intent.setClass(getApplicationContext(), MainActivity.class);
intent.putExtra("someData",id);
finish();
}
});
This will also call onActivityResult in MainActivity just fine however this next line of code will not.
gridLayout.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent();
intent.setClass(getApplicationContext(), MainActivity.class);
intent.putExtra("someData",id);
startActivityForResult(intent, 2);
}
});
I have looked into the manifest files and everything is set correctly. I've seen people suggest things such as turning android:noHistory="false" however I have none of these set in my manifest. It's very simple and looks like this.
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/CustomTheme"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ImageRollActivity"
android:theme="@style/GridTheme"
android:label=" Image Roll"
android:parentActivityName=".MainActivity"
>
</activity>
If anyone has any insight as to why this isn't being triggered that would be greatly appreciated!
In the second one you are calling
startActivityForResult()
instead ofsetResult()
(as in the first example).Calling
startActivityForResult()
will start the Activity which means it will be recreated and go through the Activity lifecycle.setResult()
is what you need for it to callonActivityResult()
.