Subjects have many “real world” applications, especially when you gradually transform your code base from an imperative to a reactive style. It can serve as a bridge between these two worlds, where you can influence a stream with non-reactive code that goes beyond the stream.
But as you asked for an example. Recently, I have been performing user behavior when a user tries to return from an action. RxJava provided me with a very elegant solution to the problem I encountered, so I needed to create an event stream that would match the user who wants to return. I deliberately avoided the phrase “press the back button, because there are several places in the code base where I can simulate a return situation, and it always goes through the onBackPressed() method.
Including this in a single thread will require massive refactoring, which is currently not in the budget. But I did not want to give up the solution through RxJava, as this could make the code much more concise. Using BehaviorSubject gave the answer, because I just needed to emit an event in the onBackPressed() method.
source share