I have a question about getting data from server.
I get general data in mainActivity of my app and I want to use it across all activities. I was thinking about using database to get my data once, save it in database and get it where I want from database. But now I'm thinking about using a Singleton class that I can use it to save data once amd get that data in every activity. Is it possible and is it a good idea?
Update :
Data Type is list of objects, So SharedPrefrences is not a good choice
and I want to save them temporally until application is running.
You can save the data in any of the following sources
Shared Preferences
SqlLite Database
Android Room or any other ORM
Files (specially for images)
Based on your data type, you can decide one among these
Have you seen the Android Event Bus? It makes communications between activities fun! Your main activity "posts" the data object once it is available. Other activities "subscribe" to receive the posted data.
Related
Fetched data is a JSON string list of Users. Let's say it has 5 string-value property and it would be less than 30 user. I thought that i could just put it in Intent, but:
Is it really the best way to do it?
What if there is hundreds of user, what is the best way?
FYI i'm using Fuel to fetch data and it could request asynchronously.
As per your question, you are fetching the data on the Splash Screen (Activity). Also, you confirm that the data can be fetched asynchronously.
I would suggest, that instead of fetching the data on Splash Screen, fetch the data directly in the activity where the data is required.
However, there could be several ways of storing the data instead of using Intent to pass data between activities. Some of the ways are as below:
Database (Room) - These are used to store data and query accordingly. You can save data in multiple tables too based on your requirements.
SharedPreferences - Best used to store frequently accessed data. This too can be used to save JSON Response.
File Storage - You can also save the data to a file for access.
You can read about storing data in Android in detail to better select a way to save and pass data.
Question: Assuming I have a web service which returns a JSON which I deserialize into a (list of) POJO(s), should each Fragment get a copy of the datasource or should I keep the datasource only in the activity and let the fragment(s) get the data via a callback interface (which I set in onAttach())? Or is there even another, better way?
WHY
I work with Android for quiet a while now but I think I still have not fully understood some very basic principles and I hope you can help me clarify things. In one of my applications, I use Volley to call a web service which returns JSON formatted data.
I use GSON to deserialize the JSON into POJOs. I then use greenrobots EventBus to send the new data around so I can update the UI. Currently all my fragments are registered as subscribers and each fragment stores a reference to the data.
I think I am doing this wrong because what if a fragment is currently not being displayed (they unregister from EventBus in onStop()). They could miss an update of the model and when a Backstack is popped, they could display an outdated model, right?
So what would be the best way to store the model so that all my UI components and controllers always display the latest version of the model? I'm fearing that one or the other component (Fragment and/or Activity) might miss an update and then displays outdated data.
What is Androids way to store models retrieved from web services and make them accessible for activities and fragments?
I think my main problem is that I can refresh the data in multiple activities and fragments. For example I could reload the data after a pull-to-refresh of a list of entries but renaming an entry would also cause an update - but only of this particular entry.
Have you thought about using sqlite to store the data and then displaying it?
What I would do is as follows:
Fetch data from service as a json
Parse the json and store it in a sqlite table
Read from the sqlite table as and when required
The advantage in doing so is that you are able to store new data as well as update the old one and as you said you can use pull down to refresh functionality, this proves helpful to store the latest data as well as the old one and always display the latest data as you are fetching from a datasource that is being updated.
I'm developing an app that downloads ParseObject's data from server and populate a ListView in Fragment with it.
I read about downloading data by Service and after it's done (some kind of listener?) it would update Fragment and be accessible until user leaves the app (which is fine by me).
On the other hand - I can just store it in Bundle and retrieve it every time I get back to that Fragment, but then I'd need implement Serializable which in this case can be cumbersome: like here
Fragments are held by DrawerLayout so it's really irritating now to see loading bar everytime You change to that Fragment and I'm looking for a solution to change that to improve UX.
What do You suggest? Which approach would be better in that situation? Maybe there are things that I should be aware of before attempting to use any of these?
I think it depends on how often the data is changed on the server.
If only daily/weekly the solution must differ compared to if the data is changed every mins/hours.
If the data is kind of static then you can download it only once, and save it to SharedPreferences, or to a seperate local file or DB.
If it changes kinda often, I'd suggest to use bundles or in memory objects, so when the user reenters the app the data should be downloaded again.
The solution i'd use would be to simply convert the entire list of data to JSON, and then save it in SharedPreferences. This allows for easy reuse of the data when the user goes back to that fragment.
As you aren't saving the data across app close/reopen, a local database is not needed.
My app pulls data from the web server. The results are given to a ListView.
Question 1: In what format should I transmit data to minimize data usage ?
Question 2: Should I use a SqlLite database to store the results from the server and feed it to the ListView or can I load it a few (say 100) values to the an ArrayList and set it as the data source for the ListView for a better performance ?
Question 1: In what format should I transmit data to minimize data usage ?
Answer 1: Its depends on your server side data format. If it is in Xml format then you have to
use xml parsing. If its in Json format then you have to use JSON parsing. I suggest to you use JSOn parsing.
Question 2: Should I use a SqlLite database to store the results from the server and feed it to the ListView or can I load it a few (say 100) values to the an ArrayList and set it as the data source for the ListView for a better performance ?
Answer 2 : You can use HashMap arraylist to store your data and retrieve as easily from it. But if you have a bunch of data earlier said by you then you need to use SQLITE database. In which you have to store your all data in it. and after that retrieve from the Database.
Q1: you can use JSON or Xml for data transferring because these both are standards but JSON is widely used for data driven applications.
Q2: For Large data you can directly load data to list using lazy loading technique, or this is totally dependent on a person, if you store in database to use this data on multiple locations and for data storage, this depends on you
Data format
The JavaScript Object Notation (JSON) is widely used in transferring data over the net
Storage
That depends on what you're trying to do - if the data is only temporary in nature, what you're suggesting works. If it needs to be stored across application starts, you will need some kind of persistence. A database would be one way to do that.
Remember though, that if you place your data loading code in your activity's onCreate or onResume method (or anywhere in startup callbacks of the activity lifecycle), it will get loaded everytime your activity is created - even if the user just flipped the device. If the data takes long to load (which over the network can always be the case, even if it's just a few bytes), this can result in very bad user experience.
One way to deal with this would be using a custom Loader, which can exist separate from your activity and would be able to cache your previously loaded data.
Conceptually, doing this would require you to extend Loader, and override the onStartLoading method to begin loading your data from the network. You should probably also override onStopLoading and onCancelLoad to keep your app from needlessly shoveling data over the connection even if it's not wanted anymore.
Having done this, you would provide LoaderCallbacks (as shown in the link I gave you), and instead of creating a new CursorLoader in the onCreateLoader callback, create an instance of your own custom loader class.
I am using asynchtasks for getting data from rest-ws and I am using these data in some activities.I see 2 main ways:
- save these temp data in activities
- save these temp data in my singletone application controller class(not mvc controller)
-(I cannot already after getting use these data's)
But I feel that these 2 ways not very good ideas.
How can I solve this problem better?
The best way to save web-data is to create content provider, fetch data in thread/service/asyncTask and insert it into content provider. Notify your activities, and activities should fetch the data from content provider. look at the following google guide lines Developing Android REST client applications
to save your data in your application is relative to many reasons like how much data are we talking about , and the type of data ...etc
take a look here , it is explained very well all types of data storage.