0

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!

4

1 回答 1

3

在第二个中,您正在调用startActivityForResult()而不是setResult()(如第一个示例中所示)。

调用startActivityForResult()将启动 Activity ,这意味着它将被重新创建并经历Activity 生命周期

setResult()是你需要它调用的onActivityResult()

于 2017-01-04T23:38:46.997 回答