21

I am trying to send data from child activity to parent. But somehow, onActivityResult(..) is not getting called. here is code

Parent activity

selectedText.setOnTouchListener(new OnTouchListener() {



    public boolean onTouch(View v, MotionEvent event) {
                    if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
                        Intent intent = new Intent(Parents.this,Child.class);
                        startActivityForResult(intent, 1);
                    }
                    return true;
                }
            });


        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            switch (requestCode) {
            case 1:
                if (resultCode == RESULT_OK) {
                    if (data.hasExtra("selText")) {
                        selectedText.setText(data.getExtras().getString(
                                "selText"));

                    }
                    break;
                }
            }

Child Activity: I can see selected value set in the setResult(). But after finish of child activity, it's not going back to parent activity.

textListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int myItemInt,
                    long arg3) {
                selectedFromList =(String) (textListView.getItemAtPosition(myItemInt));
                Intent data = new Intent();
                data.putExtra("selText", selectedFromList);
                setResult(RESULT_OK,data);
                finish();
            }
        });
Chintan
  • 906
  • 4
  • 14
  • 30
  • didn't work this after trying Intent data = getIntent() in child activity – Chintan Mar 28 '13 at 18:29
  • Try adding some `System.out.println()`'s into `onActivityResult()` to see what code is actually being executed. Once you narrow it down you should be able to figure out the problem. If not, edit your post and tell us which lines are being executed. – Don Mar 28 '13 at 18:57
  • So when i touch event is being called on parent activity..it goes into onTouch() and when it executes startActivityForResult(intent, 1) line, it goes to onActivityResult() with request code is 1. After that it jumps to Child Activity and load it. After selecting value from ChildActivity.OnItemClick()..it finish the child activity but never returns back to onActivityResult() of parent. Hope it is clear – Chintan Mar 28 '13 at 19:11
  • So, to clarify, you tried putting a `println()` in *very top* of `onActivityResult()` and nothing happened? Because what I think is happening is that `onActivityResult()` is actually being called, but either the **`switch`** statement or one of the **`if`** statements are preventing your code from executing. – Don Mar 28 '13 at 19:41
  • Yes onActivityResult() got called while ParentActivity is being executed. It goes to switch (requestCode) { case 1: but never enters into if (resultCode == RESULT_OK) {} block. because child is not yet called. As per my understanding, this method will get called when child activity set data. But it never got called again from when i finish with ChildAcitivity. – Chintan Mar 28 '13 at 19:50
  • I was having this issue due to a mistake. I was calling `finish()` right after calling `startActivityForResult()`, what was causing my caller `Activity` to not receive the result, because it had finished right after it started the other `Activity`. – joao2fast4u Jan 12 '16 at 14:24

9 Answers9

93

For anyone stuck with same problem, a symptom not to receive onActivityResult, following cases can cause this issue.

  • check you are using startActivityForResult() correctly, do not use startActivity().
  • if you do something in overriden onBackPressed method, super.onBackPressed(); has to be positioned at the last of the method, not at the first line. (my case to spend 5 hours)
  • remove android:launchMode="singleInstance" in manifest or equivalent argument to create a intent.
  • remove noHistory="true" in manifest of the callee activity.
  • check setResult() is missed.
  • finish() is called to close the activity. use finishActivity() to close callee activity.
  • use requestCode more than zero. negative value does not work.
Youngjae
  • 24,352
  • 18
  • 113
  • 198
  • My case is setting the called activity "android:launchMode="singleTask" ", and remove it is worked :) – bony Jan 13 '16 at 07:29
  • Note that `super.onBackPressed()` is not mandatory. Its the default case where you go back to the last `Activity` on the back stack, but you can replace that behavior entirely. – Sunshinator Feb 03 '17 at 17:09
  • 1
    What is the problem with negative values. I thought of assigning positive req codes to the activities I create, and negative req codes to the activities part of Android and other SDK's. Because of a negative request code it failed to call the callback. Has google provided a justification ? – Rajan Prasad Feb 21 '17 at 00:30
  • Negative value does not work should have its own upvote. That mentioned anywhere in the android documentation, btw? – somedev Aug 28 '18 at 11:57
  • 1
    @somedev yes, it is mentioned in [the docs](https://developer.android.com/reference/android/app/Activity.html#startActivityForResult(android.content.Intent,%20int)) – Vadim Kotov Jan 14 '19 at 12:17
  • @RajanPrasad yes, mentioned here in [the docs](https://developer.android.com/reference/android/app/Activity.html#startActivityForResult(android.content.Intent,%20int)) – Vadim Kotov Jan 14 '19 at 12:19
  • Also if the activity is heavy it could be killed unless you have `android:launchMode="singleTop"`in calling activity – Chibueze Opata Jul 06 '19 at 09:15
  • Oh, yes. I called `myActivity.onBackPressed()` after the `myActivity.startActivityForResult()` and this was the reason why `onActivityResult()` did not work. – James Bond Jun 16 '20 at 11:37
12

I found the mistake. I had below line in manifest.xml for child acitivity.

        android:launchMode="singleInstance"

after removing this line. it's working like a charm!!!

Thank You all for your input and suggestions.

Chintan
  • 906
  • 4
  • 14
  • 30
2

My case was a little bit complex and I realized that I used by error a static reference pointing to the first instance of the buttons listeners in a sub activity.

At the first turn it was working, but when the user was returning from the parent activity, onActivityResult was not called.

Replacing the static references was the fix

m715at
  • 21
  • 3
1

Not sure if this is your case, but when starting the child activity make sure you use startActivityForResult(intent), instead of startActivity();

Andy Res
  • 15,963
  • 5
  • 60
  • 96
  • I am using Intent intent = new Intent(Parents.this,Child.class); startActivityForResult(intent, 1); – Chintan Mar 28 '13 at 18:52
  • only extra part parent activity has below method @Override protected void onPause() { this.overridePendingTransition(R.anim.fade, R.anim.fade_right); this.overridePendingTransition(R.anim.right_in, R.anim.right_out); super.onPause(); } – Chintan Mar 28 '13 at 19:02
  • I tested your code and it works fine on my side. I'm able to pass data from Child activity and receive it in onActivityResult. Perhaps selectedItemList is empty? – Andy Res Mar 28 '13 at 19:18
  • Humm...how come it's not working on my side? i can see selected value in selectedItemList variable. – Chintan Mar 28 '13 at 19:30
  • Not sure, but try to call: `super.onActivityResult(requestCode, resultCode, data);` at the top of method. – Andy Res Mar 28 '13 at 19:33
  • i tried that also..still not working..i dont know what is causing issue. – Chintan Mar 28 '13 at 19:38
  • My question is, After finish of ChildActivity. It supposed to call ParentActivity.onActivityResult(...) but it's not being called. – Chintan Mar 28 '13 at 19:59
1

Changing my manifest for the calling activity from android:launchMode="singleInstance" to android:launchMode="singleTop" fixed the issue for me.

Francois Dermu
  • 4,437
  • 2
  • 22
  • 14
1

And intent that used to launch new Avtivity can not be set with flag Intent.FLAG_ACTIVITY_NEW_TASK, because as per android developer documentation :

This flag can not be used when the caller is requesting a result from the activity being launched.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
jinzuchi
  • 27
  • 5
1

Use singleTop instead of singleInstace as launchMode as it maintains the activity in the same task.

SKP
  • 410
  • 5
  • 20
0

onPause was effecting onActivityResult.

One of the corner case might be:
my case was different and silly: The problem was in onPause, finish called and end the activity. Removed that and works well.

@Override
protected void onPause() {
    super.onPause();
    finish(); // this line causing the issue removed it. 
}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Irfan
  • 956
  • 9
  • 16
-2

Remove OnActivityResult() method in the Main Class, it works perfectly... MainActivity->>>>Fragments

jrbedard
  • 3,662
  • 5
  • 30
  • 34
vels
  • 95
  • 1
  • 4