I had an observer for LiveData LD but the observer's onChange(Data pData) method was not being called when I made modified data like LD.getValue().modify().
I'm answering this cuz I was unable to find a solution to my specific situation.
The observer's onChanged(data) is not called when the wrapped data is modified. It's called when the wrapped data is replaced with some other data. For example
LD.getValue().modify();
Won't work but
newData = LD.getValue();
newData.modify();
LD.setValue(newData);
will call the observer.
I think you can use postValue(newData) also. Haven't tried it cuz setValue worked.
Here is the guide from where I understood this
https://developer.android.com/topic/libraries/architecture/livedata#java
Related
I am studying a project with LiveDatas on Android Studio (Java). I would like to know if it exists an Android Studio option to see where a particular LiveData is observed in the project, to see all the objects that are been notified and in which method.
you can access by typing .observe into Find in Path(cmd + shift + f in Mac):
sample live data observing:
viewModel.liveData.observe(this, Observer {})
You can't do that. Android studio can't give you such "runtime" information. The best you can do, is to call LiveData's hasActiveObservers() returns true if there are active observers, or hasObservers() returns true if there are any observer.
It seems that the best solution, to me, is to find usages of the particular
LiveData's getter method of viewModel, then see when the LiveData is observed after get.
So the question is simple: When is it best to use a ViewModel and getting data from that versus using the newInstance function on a fragment and passing the data through the creation.
I've tried googling it, but there doesn't seem to be much out there. Not anything I can easily seem to find.
ViewModel together with LiveData will store the values throughout the life cycle of the Fragment/Activity. It can also observe the underlying data so when the ViewModel changes your views will be automatically notified and updated. Read more about MVVM.
So I have a list that is updated from firebase.
In my mainactivity's onCreate, I set the listener for changes. However, the next line of code uses the list immediately. This line runs before my list is updated, thus causing an error.
What is the name of the concept i need to grasp in order to solve this problem?
Firebase Realtime Database callbacks are asynchronous. You'll need to learn asynchronous programming to deal with most of the Firebase SDKs. It means that method calls return immediately and expect to receive results some time later in a callback that was passed as an argument to the method. See the documentation for more information.
I'm a beginner Android developer. I'm still learning new things every day, and while learning I have heard this question quite frequently: What will happen if we send View object as parameter within OnCreate Method? I have searched about it but didn't find anything helpful. I just want to know is it really possible and if so then please explain the scenario.
oncreate() called when the activity is first created. This is where you should do all of your normal static set up: create views, bind data to lists, etc. This method also provides you with a Bundle containing the activity's previously frozen state.
I know similar questions have been asked multiple times. I think i read most of it. But none answer is applicable.
I need to pass complex Objects via Intents (Activity calls/Broadcasts). Everything is done within my process. That's why I see no reason to write my objects into Streams just to reassemble them a few milliseconds after. I want to pass my object reference through my application. Is there any way to do this.
Since my application will broadcast the same Event multiple times in a row I can't rely on static members. I need to get exacly the same object for what I broadcasted.
That's why I was thinking about a static "Referenceholder" that will accept an Object and return an integer that identifies this object in it's internal list so I can pass this integer via .putExtras. But as far as I know Java I could not clean up this Object from this list after it has been added because multiple Listeners could be interessted in the very same object and I would have to keep it in my Referenceholder for ever (assuming that a thread may be resumed at any time - even 2 minutes later).
Any ideas? Am I doing something wrong? Or any ideas of how I can clean up my referneces (probably after some seconds? this may lead to a crash but it seems to be more applicable than writing code that assembles and reassembles my objects for no reason)
Your options are pretty clear: There is no way to pass an un-marshallable object (Parcelable, Serializable) in an Intent. Full stop.
What you might be able to do is to pass something that is a reference to an un-marshallable object. The idea is that you would do something on the order of passing a key to a map that maps that key to the value that you are interested in passing. If both the Intent sender and the intent recipient have access to the map, you can communicate a reference to the un-marshallable object.
I don't understand, exactly, why you think static members are not what you want. I would guess that a static map, in a custom Application object, would be pretty much exactly what you want. ... and I suspect, from your comment about WeakHashMaps, that you've discovered exactly that.
... except that nothing you've said so far explains why you want to make your map Weak. Before you use a Weak map, have a look at Soft references, to make sure that that is not what you mean.
Best of luck
EDIT:
Forget about this solution. It does not work. Android is coping the Intent that you pass in .startActivity(). There is no way to get any reference inside a activity. This is - in my opinion - great bu****t my by google. You have to call your activity and place the referneces of your object in static members...
As metioned by G. Blake Meike, there is no way to pass Object references in Android via Intents. But you maybe can use WeakReferences.
A very excelent aticle about this topic is found here:
http://weblogs.java.net/blog/2006/05/04/understanding-weak-references
I got to that solution through this question:
Is it possible to get the object reference count?
So what I'm basically going to do is:
I will use Intents as a Key for a WeakHashMap and pass my Object as value. This seems to be the only Object that is suitable as Key since everything you put into the Intents extras will be serialized. Due to that, you can only pass one Object per Intent. You could implement Subclasses inside your Acitivity that can hold your Objects an put this Subclass into the map instead. But I'm still not sure if the Intent object that a receiver will get is the same that the caller created but I think so. If it is not, I will edit this solution (or maybe someone could clear that up).