onActivityResult is not called even though setResult is called

2.7k views Asked by At

I have an activity holding fragment (MainActivity.java) inside in and the fragment (ItemFragment.java) has recyclerview that shows some of items. When an item is cliked, It starts an activity (ViewActivity.java) for result. However, onActivityResult method in (MainActivity.java) is not called. Here is my codes

OnClick method in ViewHolder class that starts new activity when an item is clicked

@Override
public void onClick(View v) {
      Intent i;

      // activity is an instance of AppCompatActivity class (MainActivity.java)
      i = new Intent(activity, ViewActivity.class);

      activity.startActivityForResult(i, SyncTask.REQ_CODE);
}

Codes in ViewActivity that sends result

@Override
public boolean onOptionsItemSelected(MenuItem item) {
        ....
        resultData=new Intent();
        resultData.putExtra(SyncTask.KEY_ACTION, action);
        resultData.putExtra(SyncTask.KEY_POSITION, m.getAdapterPosition());


        setResult(Activity.RESULT_OK,resultData);
        finish();

        return true;
}

onActivityResult method in MainActivity.java

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    int action;
    int[] positions;

    if (requestCode == SyncTask.REQ_CODE) {
        if (resultCode == Activity.RESULT_OK) {
            // Handle action

        }
    }
}

Manifest file

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity android:name=".activity.MainActivity" android:theme="@style/AppTheme.NoActionBar" android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
    <activity android:theme="@style/Theme.AppCompat.Light" android:name=".activity.PreferencesActivity"></activity>
    <activity android:theme="@style/AppTheme.NoActionBar" android:name=".activity.SignInActivity" android:label="@string/sign_in"></activity>
    <activity android:theme="@style/AppTheme.NoActionBar" android:name=".activity.ViewMailActivity"></activity>
</application>

1

There are 1 answers

1
Oktay Ayar On BEST ANSWER

I figured out the problem. My request code was negative and according to documentary if request code is less than 0, result is not sent.