onActivityResult method not being called Android-ThrowExceptions

Exception or error:

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();
            }
        });
How to solve:

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.

###

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.

###

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

###

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

###

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

###

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.

###

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

###

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

###

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

Leave a Reply

Your email address will not be published. Required fields are marked *