1

I have 4 activities A,B,C,D. A startsActivityForResult B, B startsActivityForResult C, and C startsActivityForResult D. I want to do task X (dependent on result from activity B) on activity A. Currently, I use setResult()/onActivityResult() to pass forward and back the message between activities. This is fragile as activity B/C may finish at any time, in which case the message can not be passed back through setResult(). Do we have a better way to do the communication here? without dependency on activity's lifecycle.

Adrian Cid Almaguer
  • 9,385
  • 13
  • 45
  • 63
user2060386
  • 335
  • 3
  • 11
  • What kind of data are you passing? – Marcus Feb 19 '15 at 21:48
  • just some flags to indicate the result, may be string or int. – user2060386 Feb 19 '15 at 21:52
  • `This is fragile as activity B/C may finish at any time, in which case the message can not be passed back through setResult()` I am not understanding this, This is the flow for activity life cycle and thats how `setResult()` works. You can find other option like `SharedPreferences` but I think you have to figured out correct way to use `onActivityResult()` and `setResult()` accordingly Activity Life Cycle. – user370305 Feb 19 '15 at 21:57
  • Because if Activity B/C may finish any time then their should be `onStop()` `onDestroy()` will be called for these two activities and you have to call `setResult()` on this scenario. – user370305 Feb 19 '15 at 21:59
  • Yes, if B/C destroy, A won't receive the result passed from D. That's why call it fragile. Your solution works, but have to carefully handle all the cases depending on activity lifecycle. That's still not optimal. – user2060386 Feb 19 '15 at 22:46

3 Answers3

2

Since you are passing simple data types, and you want to persist these data types beyond the lifecycle of an Activity, SharedPreference is a solution. Here's an example on how to use the SharedPreference

Writing:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.saved_high_score), newHighScore);
editor.commit();

Reading:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
int defaultValue = getResources().getInteger(R.string.saved_high_score_default);
long highScore = sharedPref.getInt(getString(R.string.saved_high_score), defaultValue);
Marcus
  • 6,441
  • 10
  • 42
  • 82
0

Activity D should send an Intent starting activity A with all of the configuration parameters gathered from B->C->D. You can use an intent flag like SINGLE_TOP to return Activity A to the top of the stack. SharedPreferences will work, but you'll end up spending about as much time serializing your configuration and returning to activity A as you would just creating an Intent.

CtrlF
  • 664
  • 3
  • 8
  • 15
0

I think this answer could suit your needs well. Simply send an Intent and use the putExtra() function to send along extra data with the Intent. Then when the Intent is received, you can retrieve whatever extra data you sent along.

Community
  • 1
  • 1
vdelricco
  • 719
  • 4
  • 15