JCoder
JCoder

Reputation: 113

Android: setResult not returning result to Parent Activity

I have started a child activity from parent activity using startActivityForResult. After performing required functions in child activity I am setting result using setResult. But I am not getting result at parent activity from child activity.

Heres my code.

Here is how I call my child activity from parent activity.

 Intent i = new Intent(MainActivity.this, Child.class);
    i.putExtra("ID", intID);
    i.putExtra("aID", aID);
    i.putExtra("myMsg", myMsg);
    startActivityForResult(i, 1);

This is how I set result from my child activity.

 @Override
    public void onBackPressed() {
        super.onBackPressed();
     Intent resultInt = new Intent();
     resultInt.putExtra("Result", "Done");
     setResult(Activity.RESULT_OK, resultInt);
     finish();
}

This is my onActivityResult

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

    if (requestCode == 1) {

        if (resultCode == Activity.RESULT_OK) {
            if(data!=null) {
                Toast.makeText(MainActivity.this, "Data received", Toast.LENGTH_SHORT).show();
            }
        }
    }

}

Here the when i check for resultCode==Activity.RESULT_OK is giving false. And I also checked for intent passed outside of this if condition and its returning null.

 <activity
        android:name=".MainActivity"
        android:label="Main"
        android:parentActivityName=".MainPage"
        android:theme="@style/AppTheme.NoActionBar">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="org.mydomain.mydomain.MainPage" />
    </activity>
    <activity
        android:name=".Child"
        android:label="Child"
        android:parentActivityName=".MainActivity"
        android:theme="@style/AppTheme1">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="org.mydomain.mydomain.MainActivity" />
    </activity>

Can anyone help me to fix this issue.

Upvotes: 9

Views: 11085

Answers (4)

Ravind Maurya
Ravind Maurya

Reputation: 977

Modify your onBackPressed() method

@Override
public void onBackPressed() {

 Intent resultInt = new Intent();
 resultInt.putExtra("Result", "Done");
 setResult(Activity.RESULT_OK, resultInt);
 super.onBackPressed();
} 

Reason: Backpress operation is performed his task before sent the result to the parent activity......

Upvotes: 16

Pitty
Pitty

Reputation: 1977

Write finish() in your parent Activity.

Upvotes: 0

John Joe
John Joe

Reputation: 12803

I think you miss some of the coding part.

  1. Your code arrangment look incorrect.

  2. You have to create a variable to receive the data which was passed from childActivity.

      @Override
            protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
              if (requestCode == 1) {
                 String ReceiveData = data.getStringExtra("Result"); // you have to receiveData
                    if(ReceiveData !=null) {
                        Toast.makeText(MainActivity.this, "Data received", Toast.LENGTH_SHORT).show();
                    }
                }
            }
    
        }
    

Upvotes: 0

Frank B.
Frank B.

Reputation: 204

Try a different Intent constructor. Like this:

    Intent resultIntent = new Intent(RESULT_NAME, Uri.parse("data"));
    setResult(RESULT_OK, resultIntent);
    finish();

The reason why is because it may be looking for the data in a Uri type. Check this out.

Upvotes: 0

Related Questions