Search code examples
androidandroid-activityonactivityresult

onActivityResult NOT called in MainActivity from previous Activity


I know this is a basic question and I have seen multiple answers on it in stackoverflow but I seem to be stuck still. The onActivityResult is just not being called.

Here is my code:

1> In MainActivity I have onActivityResult

public class MainActivity extends AppCompatActivity
        implements MasterListFragment.OnImageClickListener {

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.v(LOG_TAG, "Entering onActivityResult");

    }
}

2> In my second activity I have this

@Override
    public void onBackPressed() {

        Intent data = new Intent();
        data.putExtra("myData1", "Data 1 value");
        data.putExtra("myData2", "Data 2 value");
        setResult(Activity.RESULT_OK, data);
        finish();
    }

I do a Log and the Log statement is not displayed in the LOGCAT


Solution

  • For getting result back in OnActivityResult you should use startActivityForResult(Intent,REQ_CODE) method for starting your second activity.