Android: best way to show data - android

I need an advice to the best way retrieve and show some data.
I have a service which compute and saves data from GPS (every 500ms) and JSON (every 30 sec) inside a sqlite localdb.
The data is stored in two tables: header (updated every time) and detail (add new row every time).
What I want to do now, is use this data (it is about 20-25 variables) saved in header table and bind it to an activity and some fragments.
The main activity is composed by a main fragment and a
FragmentStatePagerAdapter (with at least 3 fragments loaded inside)
Which is the best way to show this data?
I thought to:
- Create a "Bundle" and use handler and messages (Service -> Activity, Activity -> Fragments).
- Create a "Bundle" and use a local broadcast to send and receive data in every fragments loaded.
- Use a LoaderCallbacks and SimpleCursorAdapter with "FLAG_REGISTER_CONTENT_OBSERVER" in every fragment and retreive and show data directly from database.
What do you think is the best one?
(In terms of performance, best practices and low memory consuption)
Thank you in advance and sorry for my english..

in case of
Use a LoaderCallbacks and SimpleCursorAdapter with
"FLAG_REGISTER_CONTENT_OBSERVER" in every fragment and retreive and
show data directly from database.
you will do unnecessary retrieving data from database, while you can simply receive it from broadcast.
I think the best way is to register broadcast receiver and take "ready for display" data from your service.

Related

Activity (Loader - downloading) + 3 Fragments (with Loaders - computation)

I have to download "raw" data from the server every N seconds and then deliver it to fragments (which are inside ViewPager). Every fragment has to do its own computation on the data and then populate own RecyclerView.
I have one idea how to achieve that:
Activity's AsyncTaskLoader downloads data, store it and then
broadcasts an Intent that new data has come.
Every fragment has its own AsyncTaskLoader and connected broadcast receiver.
When fragment's Loader receives information about new data, get it form the activity, compute and deliver to RecyclerView
Is it the proper solution? Is there simpler approach to my problem?
You can use a Service which contain a Runnable code to download "raw" data from the server every N seconds.
Save received data in Singleton, and you can compute and deliver directly to your RecyclerView's adapter
AsyncTaskLoader in the Activity is a good idea, but I would simplify communication with the fragments by using a message bus instead of broadcasting intents.

How to pass new data to onCreateLoader

I have an Activity that implements LoaderManager.LoaderCallbacks<Cursor>. The CursorLoader feeds into a RecyclerView (think ListView). So inside onLoadFinished I swap the data inside my adapter.
Imagine the data has to do with stock performance for a day. Now I have a Spinner that allows the user to choose a day. And when the user changes day I want the data to change. So I am thinking that the Loader needs to be able to listen to the parameter. How do I do that? Per the documentation of getSupportLoaderManager().initLoader I am not sure if calling it inside my spinner’s onItemSelected will do that trick. Thanks for any help.
And when the user changes day I want the data to change. So I am
thinking that the Loader needs to be able to listen to the parameter
to me, it sounds like you want to requery your database again when those data change. you can use restartLoader. You have to possibility to provide a different id, which can be used as identifiers for the specific loader. From the documentaion:
Starts a new or restarts an existing Loader in this manager, registers
the callbacks to it, and (if the activity/fragment is currently
started) starts loading it.

Getting a reference to data collected by a Service from an Activity for visualization purposes

Hello fellow developers,
I am trying to achieve something in Android and I would like some advise on the best practice.
I have created an Activity which can start and stop a Service which collects data.
Instead of simply starting and stopping the Service, the Activity should also display the data collected by the Service.
Here in lies the problem. The data could be quite large so I would like to avoid Serializing and sending it via an Intent. Is it possible to simply get a reference to the data stored in the Service from the Activity?
Simple Example
1) Activity starts
2) Activity starts Service to collect data
3) Activity exists
4) 24 hours pass
5) Activity starts
6) Activity wants to display data collected by Service, but data is quite large.
My question again is simply this. Can the Activity get a reference to the data stored in Service or does the data have to be Serialized and sent from the Service to the Activity using an Intent?
Kind regards,
Cathal
Research android.app.Application class. Essentially you can use this for global state.
http://developer.android.com/reference/android/app/Application.html
Also, any long term data should be stored to a local database. I would try to limit the SELECT to certain number of records to reduce memory footprint.
I've heard that the Application class should not be used in this way necessarily (via Google Team). Instead you could make a separate Singleton class (For example call it "MyData") that holds a public static reference to a let's say ArrayList (For example called "list_data").
So when he activity starts it will call
MyData.list_data = new ArrayList<MyDataObj>();
Then in your Service you can add all of your data objects to this static arraylist like so:
MyData.list_data.add(new MyDataObj());
This allows you to work off of the same ArrayList without passing it around using Intents and etc. Although I would suggest doing some null checking whenevr

Sharing data between services and activities

I have a ListView with a custom ArrayAdapter, that shows orders from an ArrayList. These orders are supposed to be synced with my server. There I have an API, which I am requesting for new orders, and if there are any, I get an XML of the order back. What is the best approach to dynamically update the listView? I do not want to use SQLite to store the orders, because only last ten are supposed to be displayed. With a ContentProvider I am not able to store my custom Order object. And if I wrap the ArrayList into a singleton class and use it in the service as well as in the Activity class for the ArrayAdapter, the ListView is not dynamically updated (probably, because the arrayAdapter makes a copy of the arraylist?). Thank you very much.
Filip
use Intent or Bundle
i'm no sure what you mean regarding the ArrayAdapter not being updated, but i can give you a solution we used in my company.
I have a DataMaanger which is a bridge between the Activities and the Networking or SQLite.
The dataMaanger keeps it's data in memory so it's not in DB or on disk. the disadvantage of it is if your app gets killed for lack of memory and reconstructs itself, the dataManager will be empty, which leaves you with two options, either on every Activitie's death or you main task's activities death you serialize your DataManager's data, or if you are not dependant on any previous data, just make arequest again and update the data manager.
I use broadcasts to notify my activities.
To get an access to the DataManager i don't use a sigletone. i use the Application object, you can extend it and in the Manifest.xml give it's name in the tag, then it will be used instead of the regualr Application object.
You can access it later by using getApplication() method in Activity class.

Update model objects across different activities

Lets say I have 2 activities:
A: A ListView displaying articles titles. Data is fetched from a web server and converted from XML to a list of ArticleSummary. Only user titles and id are returned by the server. Click on a title starts activity B.
B: A form to edit an article. Article is fetched from server. When the user hits OK, modifications are sent to the server and activity closed.
When the user go back to activity A, I would like to update the article title without any additional web request.
I was thinking about the following solution:
When article is modified, send a broadcast event with article id and new attributes values.
Listen for this event on activity A
Update the ArticleSummary object
notify data changed on ListView
Is there a better approach ?
If you want to have a shared data model between different Activities, you can place it in an extension of the Application class. Or, you can use a singleton. Just reload the data from the shared location when the ListView activity is restarted.
As Fredley alluded to, if you have are communicating with a server you should be sure to do so in a separate background thread.
you can also use startActivityForResult() to launch activity and managed returned data.
Check the "Returning a Result from a Screen" section in the part below.
http://developer.android.com/guide/appendix/faq/commontasks.html

Categories

Resources