2

I have following workflow in my code. I have fragment A which starts a new activity B using startActivityForResult(intent, requestCode). Activity B has a fragment say fragment C which contains a listview (its adapter has an arraylist) and an edittext below to add new values into the listview.

I intend to send data back to fragment A. I have implemented an interface in the fragment C which is called in onPause() method of the same fragment.

@Override
public void onPause() {
    // TODO Auto-generated method stub
    OnSubtaskExitListener onSubtaskExitListener = (OnSubtaskExitListener) getSherlockActivity();
    onSubtaskExitListener.onExit(Subtasks);
    super.onPause();
}

Subtasks is the updated arraylist on adding new item from add button in edit text.

I have implemented this interface in activity B which contains the fragment C.

@Override
public void onExit(ArrayList<HashMap<String, String>> subtasks) {
....
intent.putExtra("ResultArray", stringArray);
setResult(RESULT_OK, intent);
finish();

Note that startActivityForResult was called in fragment A. Therefore, setResult has been used.

The problem: In the onActivityResult(int requestCode, int resultCode, Intent data) of fragment A, Intent data is always null. I have used android.util.Log in the onExit method in activity B to see if data being sent in put extra is null e.g. Log.d("Array Result", stringArray[0].toString()); But onActivityResult() is always getting data null.

Please help me find the bug. Thanks :)

hshed
  • 657
  • 2
  • 8
  • 21
  • did you check if the code in `onExit()` is ever called? – niculare Mar 29 '13 at 20:03
  • yes. I have used Log to check that. It's perfectly okay upto setResult(). – hshed Mar 29 '13 at 20:05
  • Does Activity A also override `onActivityResult()`? If so read [this question](http://stackoverflow.com/q/6147884/1267661). – Sam Mar 29 '13 at 20:17
  • Actually onActivityResult() is getting called. The Intent data argument in this method is null which I have ensured is not null when using the method setResult in activity B – hshed Mar 29 '13 at 20:22

1 Answers1

2

AFAIK you cannot set activity result after onPause() has been called. Setting activity result in onPause(), onStop() or onDestroy() will probably always give you null in onActivityResult().

I don't know why you want to call setResult() in onPause() method but maybe you could move it to onBackPressed() or onKeyDown() method. This should resolved your problem.

Leszek
  • 6,568
  • 3
  • 42
  • 53
  • Thanks for the tip! I moved setResult to a menu item. – hshed Mar 30 '13 at 19:21
  • 1
    I had a similar problem even though I've implemented setResult() and onActivityResult() a 100 times. My setResult() was in onBackPressed() so I was stumped why my onActivityResult() received a null intent. It turned out that called setResult() after the super.onBackPressed() call. Moving super.onBackPressed() to the end of the function solved the issue. – Monte Creasor May 04 '16 at 18:00