Search code examples
androidandroid-activityonactivityresult

onActivityResult never gets called. No obvious reason why


I am trying to pass a String from a popup activity back to MainActivity but onActivityResult is never called. I have checked every common mistake, after reading many SO topics but I haven't found any solution.

My code: In MainActivity.java

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 1) {
        if (resultCode == Activity.RESULT_OK) {
            String phone = data.getStringExtra("result");
            addPhonesAction(phone);
        }
    }
}

public void addPhonesPopUp(View v){
     Intent phoneDialog = new Intent(MainActivity.this, AddPhoneDialog.class);
     startActivityForResult(phoneDialog, 1);
}

In AddPhoneDialog.java

public void okAction(View v) {
    EditText phoneText = (EditText) findViewById(R.id.phoneDialog);
    String result = phoneText.getText().toString();
    Intent returnIntent = new Intent();
    returnIntent.putExtra("result", result);
    setResult(Activity.RESULT_OK, returnIntent);
    finish();
}

public void cancelAction(View v){
    finish();
}

Can you help me with this one? Thanks

Edit: AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.gus.uniman">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".AddPhoneDialog"
            android:theme="@style/Theme.AppCompat.Light.Dialog.Alert"></activity>
    </application>

</manifest>

Solution

  • Just to mark this as solved, I discovered later that I had done a stupid mistake, where I called the okAction when clicking "cancel" instead of doing it when clicking ok. I am sorry for being such a noob, I should have done a better job finding the issue before asking. Thanks to everyone for taking the time to comment and reply