I have an AppUtils class which handles my network requests. In my activitys onCreate i call the function that initializes a singleton from xml. All it does is make async https post request with okhttps3's enqueue to a server and deserialize the XML response to pojo(singleton).
public static void initSingleton(String apikey)
The activity also has a viewpager with 3 different fragments, the fragments all have different views and the goal is to populate the views with the data from the singleton.
What is the best way to notify the fragments when the initSingleton is done doing its work so i can update the views accordingly?
If i simply do Singleton.getInstance().getWhatever() to get a certain value in the fragments onCreateView, the Singleton is obviously not initialized yet, so it gives me a nullpointerexception.
I tried passing a Handler to the function and send a message when the request is finished. It works but i'm only able to notify the activity and not the fragments.
Would appreciate any help on this.
Cheers
edit: Is there a more elegant solution than doing it via. handler and then updating the fragment with an interface? Maybe EventBus/Otto?
edit2: Ended up realizing it with EventBus, surprisingly easy and the library is pretty small.
Otto/Eventbus can do the stuff for you.
Localbroadcast manager is also one of the option, no need of any new library dependency in this case.
Related
I have a fragment and a service that is called by the fragment.
The fragment contains a gauge, and the service uses a device's microphone to meausure sound. I would like the gauge to reflect the sound.
Is there a way for me to transfer the sound value from the service to the fragment?
I have tried to send the gauge object from fragment to the service through a parcelable, but because the gauge is from a custom library, I cannot parcelize or serialize it. I have also tried to create a custom class with just an int attribute, sending the object from the fragment to the service, hoping that updating the int attribute in the service would update the int in the fragment, but it didnt work out.
I want this update to be happening at each second of the measurement, so sending the information OnDestroy of the service does not work for me.
Do you have any suggestions on how I could do this? Thanks
For Communication between service and fragment, you have to create boundService.
Implementing TimerTask in service for each second will solve problem of updating value.
Note: we can use liveData of sound value in service and register its observer in fragment so whenever timertask update value in livedata object it will reflect in fragment.
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.
I am writing an Android App and i need to display some results from a non-activity class.
Basically my App gets the current location, activity and other things, and on those i run some functions, for example to check if the user has been at that place before.
Anyway, an intent triggers these functions, and at the end of the function I want to update a TextView with the result of these functions.
But it doesn't seem to work from anywhere besides MainActivity. I tried making the TextView static, but that doesn't seem to work, and returning the results to MainActivity is not possible either in since it is not called directly from MainActivity.
Does some have an idea on how to solve this problem?
This is my first android project, so there's still a lot of thing i don't know about. Thank you!
I don't know exactly what your code looks like, but do you have a model class that holds all your data (for example, some kind of singleton)? If so, you could just save the updated text there, and then update the TextView in a callback method.
If that doesn't fit what you're doing, I would considering refactoring your code so that the text you want is sent back to your MainActivity. You mentioned that the method is not called directly, but could your calculations pass the text to an intermediate class, which passes it to your MainActivity?
You should not update the ui drom background thread.
If you want to update the ui from background thread use Handlers,AsyncTask or use some other thread concepts.
If you want to update the UI fro Service or Broadcast receiver for eg you receive a message inside onReceive of broadcasrReceiver now to update the ui use sendBroadcast and inside your activity registet a dynamic Broadcast receiver and then update
I started to write my first mature Android application and I stuck...
I want to implement tablet view easily to I used android compatibility library v4 and fragments API. Everything was cool until I created network connection and share it beetween two fragments. You know, I have two views...
Let's assume that we have simple chat application and we need to have user list and messages list. I need to implement those both fragments depending from message received from network. So if someone is entering chat I need to update userlist fragment and if someone send new message I need to send it to messages fragment
Could anyone tell me how to do it?
Any ideas how to update both fragments with one connection.
Thanks in advance
You should have a separate CommunicationManager Class which handles all the sending & receiving - the fragments only display the information you need - all the communication logic is in this one class. Then you will have no problems with your app logic anymore.
As far as I understood you want to use one network connection (saying generally), receive a response and again display it in two different fragments.
There are some patterns you can follow to do that, but here are some suggestions to solve your problem.
Try to use the standard Android pattern where you will have:
A class for Networking. (keep it in background or executorthread)
A clsss for Repository. (It will be used to fetch the data from the Networking class). When you instantiate the Networking.class in Repository.class use a Singleton Pattern so that only one instance of Networking.class is used across the whole app which will let you to use one Networking.class to fetch all the data you need without instantiating Networking.class again.
As #Zakaria suggested, use the Android View model pattern
A One ViewModel class will be enough to use the Repository.class in it to receive data from the Networking class and share the data (Observe data) in your fragments and show your required data to user.
That's it, it will solve you problem.
I'm a little confused by the difference between Java and Android Java. Let's say I have an Activity class AndroidX. There is no main function and there is no AndroidX() constructor as we know it. I realize that onCreate() most probably initializes the AndroidX Activity, but why is there no main? What's the difference?
Consider that your activities are many *main*s and your manifest directs the execution to one of them.
Also consider that the constructor as we know it before is hidden and now it is always called onCreate()
Fair enough to keep going?
This graphic may help some.
http://developer.android.com/images/activity_lifecycle.png
In the Activity documentation they elaborate on what each function is meant for (i.e. onCreate(), onResume(), etc).
http://developer.android.com/reference/android/app/Activity.html
There is no "main" because that assumes that your app is either running or not running. But on android there are many other possible states your app could be in paused, stopped, started, etc...
Check out this link for an excellent overview of the Android Activity lifecycle.
How onCreate works is described in the Activity page of the Android Developer Reference. Specifically here:
onCreate(Bundle) is where you initialize your activity. Most importantly, here you will usually call setContentView(int) with a layout resource defining your UI, and using findViewById(int) to retrieve the widgets in that UI that you need to interact with programmatically.
In a sense you can consider this method the constructor for your Activity, as the initialization is handled there (see the Activity Lifecycle).
As for main, consider it hidden from you. Generally what you do is register listeners for UI elements such as buttons or text fields, then act on input from those UI elements. These listeners handle calls to your methods which might manipulate data or change how the UI displays.