9

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.

JCoder
  • 113
  • 1
  • 2
  • 9
  • @prashantpatil I am trying to a set result to parent activity from child activity. But its result is not being send to parent activity. I am not getting any result at parent activity. This is the issue. setResult is not setting result. – JCoder Jan 27 '16 at 17:46
  • Put your super.onbackpress() below the setresult method it will work brother – Ravind Maurya Jan 27 '16 at 18:33
  • Ya...It Worked...!!! Thanks @Ravind .. Please post your solution as answer... And can you please tell me why super.onbackpress() has to come below ? – JCoder Jan 27 '16 at 18:51

4 Answers4

16

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......

Amrut Bidri
  • 6,276
  • 6
  • 38
  • 80
Ravind Maurya
  • 977
  • 15
  • 24
  • 1
    It is not necessary to call `finish()` after `super.onBackPressed()`, since the basic implementation already finishes the activity. – JM Lord Nov 30 '17 at 20:13
0

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.

Frank B.
  • 204
  • 5
  • 17
  • Did as you told... Didnt work... Same issue...Still data is empty. – JCoder Jan 27 '16 at 18:29
  • @Jishnu Do you know if the result is not returning, meaning the result is `RESULT_CANCELED`, or if the result is `RESULT_OK` but data it passes back is `null`? – Frank B. Jan 27 '16 at 18:33
  • The result is RESULT_CANCELED. and data is null. – JCoder Jan 27 '16 at 18:41
  • Ok so then your` setResult()` is never received back in the first place. Is there room in your UI for a change where the user clicks a button or selects something and sends the result? Putting your result in the onBackPressed() may be causing some issues, but the problem is NOT in the above code if you implemented it correctly. What is your activities behavior/logic/algorithm for sending a result back? Put it in your question. – Frank B. Jan 27 '16 at 18:48
  • The issue was with putting super.onbackpress() above setResult() as pointed out by Ravind Maurya in the comments above.. Issue is solved now... Anyway thanks for helping me out Frank. – JCoder Jan 27 '16 at 18:54
0

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();
                    }
                }
            }
    
        }
    
John Joe
  • 12,412
  • 16
  • 70
  • 135
  • I tried as you said.. App is crashing with an error **Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object reference** And also please note that if condition is not statisfied when I check for resultCode . – JCoder Jan 27 '16 at 18:36
  • `resultInt.putExtra("Result", "Done");`. You want pass String Done to Result ? – John Joe Jan 27 '16 at 18:40
  • Yes. I want to pass "Done" to Parent Activity. – JCoder Jan 27 '16 at 18:42
  • And also please note that here my resultCode is becoming RESULT_CANCELED although i set RESULT_OK – JCoder Jan 27 '16 at 18:43
0

Write finish() in your parent Activity.

Pitty
  • 1,907
  • 5
  • 16
  • 34