EventBus and Observer Pattern - Android App Development

Поділитися
Вставка
  • Опубліковано 15 вер 2024

КОМЕНТАРІ • 94

  • @damax95
    @damax95 2 роки тому

    Bro I was having nightmares until I saw this video, Thanks a lot !

  • @stanna23
    @stanna23 7 років тому

    AWESOME TUTORIAL LIKE REALLLLLY AWESOME i spent months trying to understand this pattern and no one could explain it. YOU SIR ARE AWESOME!

    • @ElectronicArmory
      @ElectronicArmory  7 років тому

      +Strahinja Ajvaz Thanks! It's one of my favorite design patterns to use in my apps. Let me know if there's something in particular that you'd rather not spend months learning and I'll try to help you out! Thanks for the awesome comment!

    • @ElectronicArmory
      @ElectronicArmory  7 років тому

      +Strahinja Ajvaz You got it. I'll add it to the list of videos to do. Thanks!

  • @niks3319
    @niks3319 4 роки тому

    Perfect tutorial on Event Bus.... Glad I found it

  • @MarkOSullivan
    @MarkOSullivan 8 років тому +4

    Great explanation and demonstration of EventBus. Going to try it out now!

    • @ElectronicArmory
      @ElectronicArmory  8 років тому +2

      +Mark O'Sullivan Thanks! Glad you enjoyed it. I plan on doing a few other tutorials on awesome Android libraries that have saved me hours of dev time. Stay tuned!

  • @zuhaibahmad2463
    @zuhaibahmad2463 8 років тому +2

    Why this channel have so few subscribers? That was a brilliant explanation!

    • @ElectronicArmory
      @ElectronicArmory  8 років тому

      Best comment ever! Thanks for that! I just started the channel in January. I teach some of this stuff at a large university, so I think that helps. I also run my own mobile dev company, so I haven't had too much time to dedicate to promoting the channel. Feel free to share this video amongst your friends and get them to subscribe. I'll be extremely grateful. Thanks again!

    • @zuhaibahmad2463
      @zuhaibahmad2463 8 років тому

      Sure I will! Just don't stop and please focus on these advanced topics as there are tons of tutorials already available for basic stuff. I'm freelance dev myself and I struggle when learning new pattern or technology due to lack of proper learning material.

    • @ElectronicArmory
      @ElectronicArmory  8 років тому

      Will do, thanks! I plan on putting together another quick Android video that I'm sure you'll find interesting.

  • @rpitpatel1004
    @rpitpatel1004 4 роки тому

    Great explanation thank you for tutorials

    • @ElectronicArmory
      @ElectronicArmory  4 роки тому

      Thanks for the comment! Great to hear they helped you out.

  • @philb5426
    @philb5426 6 років тому +1

    This was an amazing tutorial, thanks!

  • @js-uy3nt
    @js-uy3nt 6 років тому

    Best tutorial on Event bus!!!

    • @ElectronicArmory
      @ElectronicArmory  6 років тому

      Thanks for letting me know. My next video will be an update to EventBus using Kotlin, so I'm glad to hear you liked this one.

  • @7NINJA
    @7NINJA 4 роки тому

    sir what if we want it for 2 bus ? then how to do this

  • @MuhammadUsman-xi7ou
    @MuhammadUsman-xi7ou 6 років тому

    Amazing lecture found ever...

    • @ElectronicArmory
      @ElectronicArmory  6 років тому

      Thanks! I'm currently updating this video for Kotlin, if you're interested in learning Kotlin on Android. Thanks for the comment!

  • @swapnilgawali9412
    @swapnilgawali9412 6 років тому

    best video on eventbus, thanks man

    • @ElectronicArmory
      @ElectronicArmory  6 років тому

      Awesome! Very happy you think so. I love EventBus. I use it in all my Android apps. Thanks!

  • @xDudek13lx
    @xDudek13lx 8 років тому +2

    This tutorial is lovely

  • @kde6769
    @kde6769 5 років тому

    Great video! Tnx

    • @ElectronicArmory
      @ElectronicArmory  5 років тому

      You're welcome! I have a Kotlin version of this video too if needed. Thanks!

  • @baksoy
    @baksoy 7 років тому

    At 4:21 you stated "it has to be called onEvent". CORRECTION: Version 3.x: Event handling methods must be annotated by @Subscribe, must be public, return nothing (void), and have exactly one parameter (the event). In Version 2.x it was as follows: By convention, event handling methods must be named "onEvent", be public, return nothing (void), and have exactly one parameter (the event).

    • @ElectronicArmory
      @ElectronicArmory  7 років тому +1

      Yep! Thanks for pointing that out. The newer version has slightly different requirements. I'll be revisiting this video and likely recreating it with the new info.

  • @saqibmasood6778
    @saqibmasood6778 4 роки тому

    Hi, Thanks for the nice tutorial.
    Can you please tell me where can we get the list of all the @Subscribe methods and their usage.
    Also what is the difference if data is send and subscribed by say FragmentsA and B, which do not exist at the same time.

  • @amadain17
    @amadain17 3 роки тому

    Great tutorial. I noticed the message "The application may be doing too much work" in your logs. Is this an eventbus message and is it important?

    • @ElectronicArmory
      @ElectronicArmory  3 роки тому

      Good catch. Not sure what it was for but I doubt it was the fault of Event Bus as it's pretty optimized. Might have just been the computer I was using, being slow.

  • @sumitroy7444
    @sumitroy7444 7 років тому

    I am using a bottombar with three fragments associated with it. The first fragment uses a recycler view . My question is how to pass values which I receive from the recycler adapter to the third fragment using eventBus?

    • @ElectronicArmory
      @ElectronicArmory  7 років тому

      That data should live in a third class, preferably a singleton, where you can gain access to it from any class. You can keep a copy in the View's adapter, but it shouldn't be the main repo. The third fragment then can access the data in that singleton at any time, especially when it gets notified that there's new data to get.

  • @rishsethia
    @rishsethia 7 років тому

    Awesome tutorial !

  • @madhankanakamani6144
    @madhankanakamani6144 8 років тому

    nice explanation . thanks for making this video :)

  • @l201012
    @l201012 8 років тому +2

    Why Eventbus better than a normal interface callback? I think this is the same..
    Callback hell?

    • @ElectronicArmory
      @ElectronicArmory  8 років тому +1

      +László K Szia Laci. Thanks for the question! Too many callbacks is one. But with EventBus objects don't need to know about each other. They can also send messages to multiple objects at once. You can accomplish the same things with callbacks but it messy and hard to maintain. It's one main reason for "spaghetti" code. Hopefully that cleared it up some. Let me know if you still have questions!

    • @l201012
      @l201012 8 років тому +2

      Thanks, what about when we have more activities, that listening to the same things... Am I have to unregister in the activity that stopped and start again when started?
      How can we handle this?

    • @ElectronicArmory
      @ElectronicArmory  8 років тому

      That's exactly what you have to do. Take a look at the getting started to see how to register and unregister event listening in the onStart and onStop methods: greenrobot.org/eventbus/documentation/how-to-get-started/

    • @l201012
      @l201012 8 років тому +1

      OK, it is very simple. What is better? Otto, greenrobot, or RxBus?

    • @ElectronicArmory
      @ElectronicArmory  8 років тому

      Never used RxBus but from the quick look at it, it doesn't look as great as the other two. I began using Otto first, but switched to Green Robot's Event Bus since Otto doesn't notify you in the main thread. Here's a comparison chart github.com/greenrobot/EventBus/blob/master/COMPARISON.md
      I mostly use EventBus for updating and keeping the UI thread synced, so getting notified on the Main thread is important to me. That chart might be biased though.

  • @aviksutar
    @aviksutar 6 років тому

    Thank you.Can you show how can i use eventbus replacing onactivityResult while using camera etc

    • @ElectronicArmory
      @ElectronicArmory  6 років тому +1

      Sorry for the late reply. I'm not sure why you would want to replace onActivityResult, but you can always post an EventBus event in the onActivityResult callback. If you really don't want to do that, I suggest creating a custom camera activity instead of using the system's built in camera activity. This gives you a lot more flexibility but requires more coding. Let me know if that answers your question. Thanks!

  • @rpitpatel1004
    @rpitpatel1004 8 років тому

    nice explanation thanks

  • @markfernandez7113
    @markfernandez7113 7 років тому

    Easy Peasy! Awesome tuts.
    Though I bumped in to this problem, where my receiving Fragment has a method postMethod() and what it does is saving users post, The problem is the sender Fragment is a map, where I am getting the latitude of the location so this fragment initially fires the value of latitude and the receiving Fragment receives the value initially, but knowing the postMethod is the one responsible for saving users post the value can't be receive because the sender Fragment is already dismiss, Any thoughts or solutions for this? Thanks in advance :D

    • @ElectronicArmory
      @ElectronicArmory  7 років тому +1

      Great question. Thanks! I have a simple answer, but it'll require a bit more work on your side. Instead of getting the GPS coordinates from the map fragment, you'll have to request them from the LocationManager using Android's location services. I have a video on that but I think it's in one of the Android live streams I did. Here's some documentation on it in the mean time: developer.android.com/guide/topics/location/index.html
      The reason you'll want the service and the location manager is, you'll want to store that information in a class that lives throughout the application lifecycle, namely a singleton that gets instantiated at application launch. I should have a video coming out soon that will handle all of this, including the use of EventBus. So stay tuned! Thanks for the comment!

    • @markfernandez7113
      @markfernandez7113 7 років тому

      Wow! Didn't expect for the fast reply, anyways thank you so much! You've earned my sub and definitely going to stay tune to your upcoming vid. tuts. More power to your channel @Electronic Armory!

  • @JamesBond-mq7pd
    @JamesBond-mq7pd 7 років тому

    Tutorial good. But i was trying figuring out how to handle with EventBus when your Activity dead and you have to send Notification to users if Activity dead. But when your Activity alive you don't have to send Notification. and i tried to do all this things through EventBus but i didn't do this. so difficult

    • @ElectronicArmory
      @ElectronicArmory  7 років тому

      Store all of that information in a separate object. Then when your activity loads up, check that separate object that is storing that data. Don't store any data you need to share in an activity. Thanks for the question!

  • @shivamdawar97
    @shivamdawar97 6 років тому +1

    great

  • @jorgereina1077
    @jorgereina1077 7 років тому

    THANK YOUUU!

  • @mohsaniqbal1259
    @mohsaniqbal1259 5 років тому

    hello please kindly tell me how to call another activity (child activity ) by using greenrobot in android

    • @ElectronicArmory
      @ElectronicArmory  5 років тому

      In the onEvent callback, simply create a new intent and launch it as usual. You'd have to be careful with this as you'd have to make sure where it's getting called is going to still be around to call the code.

  • @mohameddawood572
    @mohameddawood572 7 років тому

    it is possible to make onEvent() two times in the same class ??

    • @ElectronicArmory
      @ElectronicArmory  7 років тому

      Yes. Simply change the event type that is a parameter for that function to overload the method and have multiple onEvent methods for different event types.

  • @darrengreene8327
    @darrengreene8327 7 років тому +1

    Amazing tutorial. Thank you very much!

    • @ElectronicArmory
      @ElectronicArmory  7 років тому +1

      Glad you liked it! I have a bunch more Android videos planned, just like this one. Stay tuned!

    • @darrengreene8327
      @darrengreene8327 7 років тому +1

      Can't wait for more content :)
      Could you please make a tutorial on how to use EventBus on fragments in Swipe views(View Pager)?

    • @ElectronicArmory
      @ElectronicArmory  7 років тому

      Sure. You would just register each view with EventBus as it was instantiated. Then each one of them would get a notification, assuming they're all the same subclass and implemented the same way. Then, don't forgot to unregister them as they get destroyed, but that's the basics of it. Were you looking to do anything specifically with it?

    • @darrengreene8327
      @darrengreene8327 7 років тому

      Yes.I have 3 fragments in an Activity: FragmentA, FragmentB and FragmentC (all different subclasses). FragmentA has 1 EditText and FragmentB has a Button. I am trying to send EditText data from FragmentA to FragmentB which would be displayed as a Toast when the Button is clicked in FragmentB. I am not very familiar with how fragments work, I'm a noob at Android :P

    • @ElectronicArmory
      @ElectronicArmory  7 років тому +1

      That scenario would have been a better example to make a video on. Perhaps I'll find time to do that since it's a great illustration of EventBus.
      FragmentA registers to receive a notification. Let's call that ButtonPressNotification. In FragmentB, when that button is pressed, in your Click Handler, post that ButtonPressNotification notification to EventBus. When FragmentA receives that notification in its notification handler, simply create a toast from FragmentA. If FragmentC needs to know too, either register the same notification, or, create a second notification class, say ButtonPressedAndProcessedNotification (or something better), and post that from FragmentA, after the toast.
      Finally, if you need to have this handled on multiple views, in a pager, post the notification, but attach the index of the slide with the notification. That way, when the other views/fragments get that notification, they can check to see if that notification is for their view. If not, they can ignore it. If so, they can handle it like normal. That way you can keep it dynamic.
      Hope I made that clear! Let me know if you need me to elaborate on anything. Thanks!

  • @massivedeveloper3626
    @massivedeveloper3626 7 років тому

    what about performance , dose event bus cost or not ?

    • @ElectronicArmory
      @ElectronicArmory  7 років тому

      +Massive Developer The performance is great and better than doing it manually by keeping your own references to subscribers. Just make sure you unsubscribe when the instance no longer needs to know about the event, per the documentation. Great question! Thanks!

  • @apoorvakartikey4032
    @apoorvakartikey4032 7 років тому

    there is an error:could not download eventbus.jar(org.greenrobot:eventbus:3.0.0): No cached version available for offline mode

    • @ElectronicArmory
      @ElectronicArmory  7 років тому +1

      Make sure you uncheck Offline work in Preferences or File -> Settings -> Build, Execution, Deployment -> Gradle -> Offline Work
      Once unchecked, restart Android Studio and make sure you resync Gradle. Also, make sure you copied and paste the Gradle dependency line from GreenRobot's documentation since it could have been updated.

    • @apoorvakartikey4032
      @apoorvakartikey4032 7 років тому

      Electronic Armory again it is asking to switch it on stating unknown host 'jcenter.bintray.com'.you may need to adjust the proxy setting in gradle

    • @ElectronicArmory
      @ElectronicArmory  7 років тому

      Not seeing any issues. Sounds like some kind of strange network error. Try cleaning the project, hitting that host directly or seeing if the proxy settings are correct, or turned off.

  • @leonidustenko6510
    @leonidustenko6510 8 років тому

    Thanks!

  • @shoaibmirza4884
    @shoaibmirza4884 6 років тому

    Thnx buddy .. 👍🏻

    • @ElectronicArmory
      @ElectronicArmory  6 років тому

      Glad you enjoyed it! I have a Kotlin version of this one coming up soon.

  • @radosawjuszczyk638
    @radosawjuszczyk638 8 років тому

    This example has a memory leak becouse you did not unsubscribe it in on destroy. What is more an activity will not be notified about new event if it will be destroyed by the system while ChildActivity is launched.

    • @ElectronicArmory
      @ElectronicArmory  8 років тому

      Thanks for pointing that out. I forgot to unsubscribe to the event in the onStop, or similar life cycle method. I'll add an annotation to the video.
      It's not a memory leak however since no memory is being allocated when you register the class. Event Bus keeps an internal array of all the objects and their registered notifications. If one of the objects in the array has no reference, since it was deallocated, Event Bus moves on.
      You're also right about the parent activity not being notified if it gets destroyed when the child appears. That's something that's rare, but can certainly happen (all the time if you enable that feature in the simulator). However, the observer pattern should not be relied on to relay mission critical data but instead should only be used for syncing or reacting to real time events as they happen. If you have a background activity, when it resumes or is recreated, it should check a static object, say a singleton, to see if any data updated in it's absence. I'm working on putting together a video that illustrates that even better so make sure you stay tuned and keep providing the critical feedback to me. I appreciate it!!! Thanks!

  • @SahilRallySuperStar
    @SahilRallySuperStar 5 років тому

    What happens if the observer is killed when event is sent.

    • @ElectronicArmory
      @ElectronicArmory  5 років тому

      Good question. EventBus keeps a list of observers that it'll send that event to. If that observer doesn't exist, it gets ignored. However, if you have a long list of observers that don't exist, it could slow down performance. So unregister the observer in the onPause or onStop.

    • @SahilRallySuperStar
      @SahilRallySuperStar 5 років тому

      @@ElectronicArmory But what if Notification is important and cannot be ignored. Is that Notification received when Activity again comes back to life.

    • @ElectronicArmory
      @ElectronicArmory  5 років тому +1

      You can unregister the Activity when that Activity is destroyed in the onDestroy so that the subscriber will always receive the notification. EventBus allows you to get those notifications on different threads, so if the activity is not running, you can get it in a background thread, store the value in a member variable and then when in your onResume function, use that member variable to perform the work on the main thread so you can update the UI. But if you want to make absolutely sure the data is processed, you'll want to make a direct call to a controller or manager that controls that data. For example, if EventBus is telling me I there's a new piece of data, or even if it doesn't, on my Activity's onResume, I can go to the controller that holds that data and see if there's anything new there, or request data that's new within the last few minutes, or whatever your logic needs to look like. While EventBus should work to ensure you get the notifications, if you're using it to send hundreds, there's a possibility the notification doesn't get sent, or is delayed so it's always a good idea to ensure you have the latest data from a central data authority.

    • @SahilRallySuperStar
      @SahilRallySuperStar 5 років тому

      @@ElectronicArmory Thanks a bunch ! :-)

  • @user-hw7xu8xf4q
    @user-hw7xu8xf4q 8 років тому

    very good. could you post your presentation?

    • @ElectronicArmory
      @ElectronicArmory  8 років тому

      Absolutely! I posted a link at the bottom of the page: www.electronicarmory.com/articles/eventbus-android-tutorial-observer-pattern

    • @user-hw7xu8xf4q
      @user-hw7xu8xf4q 8 років тому

      Thanks

  • @guycohen1910
    @guycohen1910 8 років тому

    I think EventBus is more like Broadcast receivers than Interface...

    • @ElectronicArmory
      @ElectronicArmory  8 років тому

      Yes. Great comment. EventBus is easier to use than the built in broadcast receivers. Green Robot actually addresses this on their site in the FAQ:
      Q: How's EventBus different to Android's BroadcastReceiver/Intent system?
      A: Unlike Android's BroadcastReceiver/Intent system, EventBus uses standard Java classes as events and offers a more convenient API. EventBus is intended for a lot more use cases where you wouldn't want to go through the hassle of setting up Intents, preparing Intent extras, implementing broadcast receivers, and extracting Intent extras again. Also, EventBus comes with a much lower overhead.

  • @sanamshresthaAI
    @sanamshresthaAI 7 років тому

    Thanks

  • @nuniezjorge
    @nuniezjorge 4 роки тому

    i prefer this than react, it doesn't introduce new syntax

    • @ElectronicArmory
      @ElectronicArmory  4 роки тому

      Me too. I started using it since the syntax as so simple, straight forward and easy to understand. It's also extremely flexible with the built in annotations.

  • @CnaKhn
    @CnaKhn 3 роки тому