Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onActivityResult method not being called Android

Tags:

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();             }         }); 
like image 760
Chintan Avatar asked Mar 28 '13 18:03

Chintan


People also ask

How can we call fragment onActivityResult from activity?

To get the result in your fragment make sure you call startActivityForResult(intent,111); instead of getActivity(). startActivityForResult(intent,111); inside your fragment. @StErMi Make sure you call startActivityForResult() and not getActivity(). startActivityForResult() from your fragment.

How does onActivityResult work on Android?

class); startActivityForResult(i, 100); 2.In your secondActivity class write following code for onClick Event For ex: In secondActivity if you want to send back data: Intent intent= new Intent(); intent. putExtra("result",result); setResult(RESULT_OK,intent); finish();

What is the replacement for startActivityForResult?

Among the contracts available, the StartActivityForResult contract is the replacement to the startActivityForResult . The callback is to be called once the activity result is available.


2 Answers

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.
like image 96
Youngjae Avatar answered Oct 02 '22 14:10

Youngjae


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.

like image 41
Chintan Avatar answered Oct 02 '22 12:10

Chintan