What is the best place in fragment lifecycle to call REST service for example to fill ListView with received data ? There are onCreateView, onActivityCreated and onResume. My first thought was to call network service as soon as possible, so on the end onCreateView, but i'm confused about this.
Just don't bind REST calls to your UI. You should split up UI and business logic responsible for getting and updating data. UI is just a facade so you shouldn't try to call any network services from UI. You can implement any of the patterns described by Virgil Dobjanshi in IO Talk
Related
I have started a thread which connected to bluetooth device and streams data. I actually started this thread from a service(Thread not started from activity). So know I need to communicate to activity to show the list of input streams. I am not sure if event bus can provide a such a high frequently changing data. Can some one give me a better existing examples of how to use display the streams.
Please don't ask me for the source code.
I believe Event bus can be a good solution, also check bound services:
https://developer.android.com/guide/components/bound-services.html
I advise you to look into LiveData from the Android architecture components. You can observe the result from the activity, and it will "unsubscribe" for you when needed (onStop()). You can read about the advantages on the link I provided.
You could extend a LiveData and implement the logic inside it (like this example), or just use it to dispatch the updates, then observe it on the Activity. The postValue() method will ensure that it gets dispatched on the MainThread.
I have application where I have Activity with few Fragments. In that state it's easy. In Activity, using retrofit I make request to REST API, get data then pass it to Fragments using bundle. (Passing list of my objects using bundle was a little pain, but manageable)
Problem starts when I want refresh data in Fragment every x seconds. With described solution I'd have to recreate Fragment with every request, which is ... stupid.
So I have 2 ideas:
make all requests to REST API in Fragment. But I have no idea if it's considered as good solution.
make all requests in Activity and exchange data using EventBus library. (http://greenrobot.org/eventbus/)
Can you please tell me if one of those ideas is good, or should I do something completely different? Or can you tell me pros and cons of different approaches?
Thanks a lot for answers!
Make your fragments loosely coupled with activity so that it can be used with other activities. According to Google -
In order to reuse the Fragment UI components, you should build each as a completely self-contained, modular component that defines its own layout and behavior. Once you have defined these reusable Fragments, you can associate them with an Activity and connect them with the application logic to realize the overall composite UI.
Read more - https://developer.android.com/training/basics/fragments/communicating.html?hl=ru
It is better to request network data from Fragment. Also if you have any dependency of the data in your activity which is received from the network call then you can also use callbacks to send that data back to activity
It's better to have service for HTTP calls. You can make async calls from service, and implement listener interface at your activity or fragment. Than that listener can interact with view.
I usually agree with MmtBkn, but since you use Retrofit, I think it is at least not a bad practice to perform HTTP requests from the UI class. And in my opinion you should make these requests from the Fragment, because it contains the views where the loaded content will be displayed. If you think about placing the Fragment in another Activity, you would end up with duplicate code that performs the HTTP request and sends the data to the Fragment via event bus.
In the App i am developing, I have to connect to a server/broker to send and receive some messages to and from the broker, and also I have to provide a callback synchronous and an asynchronous listener. As far as I understand, such operation should be better placed in onResume(). am I right? please guide me and confirm whether onResume is the best place to connect to servers/brokers with callbacks or they woud better be inside other lifecycle callback?
As you probably will do something with your UI as soon as you have the answer from the server, you'll want to contact it only when your UI has been created and set up.
OnCreate is fine in an Activity (after setContentView and assigning your Views), OnViewCreated for a Fragment. As long as you're 'prepared' to receive the server's answer instantaniously, it doesn't really matter.
Remember that most 'startup' lifecycle callbacks will be called again if the screen rotates, so don't do the server call twice if you don't have to.
I'm creating an app in an MVC pattern... and I'm trying to do most of the data getting and manipulation on the Model side of things so I don't clutter up the Activities with code that does not belong there, but the one thing that's tripping me up a bit is AsyncTask... from what I've seen it seems that this is called most of the time in the UI because it allows you to update. the problem with that is I am doing all my network calls using AsyncTask from my model not my activity. But I still want to update my UI when cretin things happen such as a network call returned something put it in the ui.. so is there a way to make a call back from a model to the Activity or something to that effect, so I don't have to put that code in the activity?
any help would be greatly appreciated.
What you need is an interface which acts as an update "listener" in your Model. That way, your activity can register with the listener when it is active and receive notifications of changes accordingly.
There are many examples (and built-in listener classes) - here is one:
http://www.tutorialspoint.com/java/java_interfaces.htm
Creating an interface in your Activity that is registered in your model allows your model to notify the interface of changed data in order to update properly. If you implement your model as a service or maintain instances of them in a service, then you can bind to the service and then register your listener assuming your model processing extends beyond the life of activities.
If not, AsyncTask is where model processing should occur and you can implement your model synchronously and use listeners to monitor it.
I have a class that extends service and the service basically fetches data from the cloud and lists ot in a listview..am getting an error when i try to use "findviewById" method to get the listview because the class doesn't extend Activity.does anyone know how i should go about it.
Your service cannot modify the UI directly. In fact, there may not be a UI at all, as the user may have pressed BACK and destroyed the activity while the network I/O is going on.
Instead, you need to send a message from the service to the activity to let the activity know, if it exists, that there is new data. For this, you can use:
LocalBroadcastManager from the Android Support package, or
a third-party message bus implementation, like Square's Otto, or
a Messenger tied to a Handler
etc.
Hi There in my opinion better to use the AsncTask in that you have to write the backend downloading data part in doinBackground method and setting the data to the list view in onPostexcute.
Better have a look into the asynctask
http://developer.android.com/reference/android/os/AsyncTask.html
mean while try to bind a service and make communication from service to activity for this have a look into how to bind a service.
for binding a service
http://developer.android.com/guide/components/bound-services.html