I want to implement a Navigation View
with many fragments that depend totally on a value defined in the MainActivity
. I know that variables in MainActivity can be accessed using method defined in MainActivity from other Fragments to get the value, but the catch here is that the value of the variable in MainActivity may change (which runs on an AsyncThread). Now, I either change the code such that my Fragments update their value based on some event in the fragment itself or use SharedPreference
. But I don't want to use SharedPreferences, neither have to check for change in the value unnecessarily many times.
I know in RxJS, we use Observable that runs Asynchronously and works in a fashion similar to a conveyor belt. A bit of googling through the official docs : Observable confirmed my suspicion of something similar being available in Android, but couldn't find any proper Tutorial or explanation on how to implement it. So, am looking for a simple code snippet that might give clarity to how Observable works in Android and if it is Async and if its based similar to RxJS implementation of it. (No, I don't want RxJS implementation)
Test Case:
MainActivity : int a, b (need observable for both variables)
Frag1 : int a1 , b1, a1changed(),b1changed()
Frag2 : int a2 , b2, a2Changed(), b2changed()
MainActivity contains integers whose value when changed should reflect in corresponding integers across the Fragments and calls separate function for each Fragment on the change being noticed.
There is an good example about using Observable of Android (java.util.Observable) here: https://andhradroid.wordpress.com/2012/04/05/object-observer-pattern-in-android/
And another example about using Observer pattern in Java: http://www.journaldev.com/1739/observer-design-pattern-in-java.
Generally, there are two ways:
I like the second way more, for example: (Sorry, I want to make sure it works so I make a complete example)
The Observable:
The Observer:
The MainActivity:
The Fragment1:
The Fragment2:
The Adapter:
The main layout activity_main.xml:
The fragment layout fragment_basic.xml :
Here is a simple example with an
Activity
and a singleFragment
but it will be the same with other fragments.First you need to create a class standing for the value you want to observe, in your case it's a simple
int
so create a class containing thisint
and that extendsObservable
(it implementsSerializable
to simplify exchange between activity and fragment):Then use this observable int in an activity (activity layout contains a
Button
and aFrameLayout
used to display aFragment
):Finally, create a
Fragment
that listen a value change:I try to name my variables the same as yours, I hope it will help you.
Reactive is not part of Android but you are probably looking for this library: https://github.com/JakeWharton/RxBinding
The landing page is missing an introductory example, so you have to look at the javadoc. This post should give you a good start: How to create an Observable from OnClick Event Android? Here is the code sample from Matt to get you started