Search code examples
androidandroid-activityonactivityresult

onActivityResult not being called?


I am starting ProfileActivity from HomeActivity's toolbar. For some reason when I return from ProfileActivity, the onActivityResult() isn't being called. Below are the relevant code. Thanks in advance!

Toolbar code inside HomeActivity:

   @Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.searchBox:

            return true;

        case R.id.userProfile:
            Intent intent = new Intent(HomeActivity.this, ProfileActivity.class);
            Bundle b = new Bundle();
            b.putString("userID", uID);
            intent.putExtras(b);
            startActivityForResult(intent, 10);
            finish();
            return true;

        default:
            return super.onOptionsItemSelected(item);

    }
}

onActivityResult() inside HomeActivity:

    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 10) {
        Log.d("WorkoutLog", "onresult called");
        if(resultCode == Activity.RESULT_OK){
            Toast.makeText(context, data.getStringExtra("date"), Toast.LENGTH_SHORT).show();
        }
    }
}

Method called in ProfileActivity to return back to home:

    public void editWorkout(String date){

    Intent intent = new Intent();
    intent.putExtra("date", date);
    setResult(Activity.RESULT_OK, intent);
    finish();

}

HomeActivity and ProfileActivity manifest:

        <activity
        android:name=".HomeActivity"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="adjustPan">
        <intent-filter>
            <action android:name="android.intent.action.HOME" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name=".ProfileActivity"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="adjustPan">
        <intent-filter>
            <action android:name="android.intent.action.PROFILE" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

Solution

  • finish() effectively kills the calling Activity, do not call it after startActivityForResult():

    case R.id.userProfile:
        Intent intent = new Intent(HomeActivity.this, ProfileActivity.class);
        Bundle b = new Bundle();
        b.putString("userID", uID);
        intent.putExtras(b);
        startActivityForResult(intent, 10);
    
        return true;