2

As a new android developer,it seems to me that passing data from one activity to a fragment or a fragment to an activity is too much code to write. Wondering can otto library can be used as a replacement of intent for passing data ? Can it be used as one and only data passing mechanism in android apps?

rat
  • 143
  • 1
  • 7

2 Answers2

2

I suppose that it might be possible, but it wouldn't be code that I'd want to have to maintain.

The use of a message bus assumes that the sender and the recipient of the message exist and are active at the same time. In the case of transitioning from one Activity to another via an Intent, the destination Activity typically does not exist, and if it does, it is not in an active state. So, in this mode, it would not be possible to replace an Intent with a bus message.

Otto does support "producers", which could be used to do this, but again, I really wouldn't suggest it.

If you are having to pass lots of data between Activities, then you probably have some design issues. It's generally an indication that you have stuffed everything into your Activities, instead of having a proper, separate data model.

GreyBeardedGeek
  • 29,460
  • 2
  • 47
  • 67
  • just curious, have you a relation with Linus Torvalds?.. You just like him without your beard. – ישו אוהב אותך Jul 13 '16 at 02:52
  • Thanks for your helpful answer.From your answer,I understand it would be possible to do such things with otto but for this, I have to write some code that would be hard to maintain. Actually I didn't find any solid patten for these data passing and bundling issue on android. Like activity to activity is one way but for device rotation savedInstanceState have to be managed and there is fragment to activity, fragment to fragment data passing issue. Is there any resource out there for guideing us through all these data passing machanism. @GreyBeardedGeek – rat Jul 13 '16 at 12:04
  • I think that the only solid resource that you are likely to find is the official Android docs on how to pass data through Intents. I have successfully used Otto for Activity <-> Fragment communication (which also helps to deal with the Fragment Lifecycle), and other communication where all parties to the communication are assumed to be either registered to the bus (ready to act on input), or not registered. The one place that this does not work well is Activity <-> Activity, as only one is active (should be registered to the bus) at a time. – GreyBeardedGeek Jul 13 '16 at 17:38
2

Wondering can otto library can be used as a replacement of intent for passing data ?
Yes, we could use otto (or other event bus) as means for passing data.

Can it be used as one and only data passing mechanism in android apps?
Can be yes or no. Depends of your necessity. If you working with activity and fragment, there you use Intent and Bundle. But when you want to communicate between activity and fragment, you can use otto.

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96