I’m developing an android app where the user will brows different activities. I have a RecentActivity.java class that will show an empty listview and every time the user enters other activities, I add each activity to the listview in RecentActivity.java . But when I exit the app and open other apps the RecentActivity loses the content were stored in the listview, so I wanted to keep its data all the time. Thanks I will appreciate your suggestions.
You can create an SQLite database to store your browsing history, here is a tutorial of SQLite usage in Android:
Android SQLite tutorial
There are a number of different Storage Options but which to use depends on what you are storing.
"State" is rather vague, you can use shared preferences if you are storing primitive data
You can use the JSON format and something like the GSON library if you want to store classes but don't need query capability (GSON library makes it very easy to use in most cases) and then write the results either external or internal storage (up to you)
SQLite in my opinion would be overkill for simple storage of state, but then again not exactly sure what you are trying to store or how you are trying to retrieve it.
Related
I am building an app as part of a project and I am stuck at the moment.
Background: My team is making a "Time-Wasting" App, and the user can press buttons such as "study" to track their time spent on activities - so there will be lots of integers values in the growing ArrayList.
Task:
Store a growing Arraylist(integers) in the app store and retrieve the values for calculation and graph display purposes.
Problem:
I have looked so far how do to this, but I am a bit stuck. Do I use SharedPreferences, the internal app storage or something else. Also, I have a hard time finding code that I could look at and follow what it is doing.
I need:
An efficient way to store the ArrayList (integers) in a storage place and retrieve it for my app. Any suggestions what would be best and where would I find the code for that?
I looked for about 2 weeks through videos and a through stack overflow but I am still stuck and not closer to how I should store this data type in my app.
Thanks, any help much appreciated :)
My opinion avoid the array list and use a database.
It will allow your app to grow including storing extra information along with the time waster including when they chose to do it, how long , etc.
I recommend Room Database it is an ORM for Sqlite it is super easy to use and there is plenty of documentation on implementation as it's apart of the Android Jet Pack.
If you are really stuck on using a list you can use SharedPreferences it doesn't handle ordered lists but you can store your list as a json string.
I'm currently developing an android app in which I'm dealing with php files that returns json, the json then gets parsed and is used to fill up an arraylist of different objects.
What I'm trying to do is, I want to cache these objects in case of no internet so I can reload them and make the application useable offline.
Currently I have 2 solutions for this:
A) Make an SQLite database and creare tables with the same structure as the objects I'm trying to cache and then reload them on startup
B) Save the JSON strings inside the shared prefs and parse them on startup.
I didn't really find any best practices or tutorials when I came across data caching so I'm lost now and I have no idea what to do. So if you guys can please help out I'd be thankful.
You might find the following talk interesting:
https://www.youtube.com/watch?v=xHXn3Kg2IQE
As you can see, a lot of scaffolding goes into the ContentProvider approach. For a very simple app, it can be acceptable to just cache some JSON in a SharedPreferences. However, as your app becomes more complex, the advantages of the ContentProvider will be worth it.
You can use files instead of sqlite and shared preferences.
Shared prefrences is using for small data.
Sqlite is using when you need to make queries on stored data.
So I think it's better to cache in files, or if you are using something like retrofit or any network lib you can check if they support caching and use it.
Definitely not option B. Shares preferences are not suitable for storing large amounts of data. It's XML so cannot be queried like Sqlite. The time to retrieve a single item will increase linearly with the amount of data stored. Secondly JSON will have special character these will need to be escaped which means the storage size will increase even more.
There is however an option C. Using cache files. This approach and other store options available to you are described in the google developer guide and this is essential reading.
So in summary: you options are to parse the JSON and store it in sqlite or to save the json as a file in the cache directory.
I want to write an android calculator app like the one on my android phone. It saves history for operations and by clicking a button it shows last operations. Now my question is what is the best way to save operations? Is it reasonable to save them to a file in internal storage or what?
There's some options..
1) Include a SQLite Database, as others mentioned. This makes storing lots of information really easy. You can find tutorials on how to include one properly in your project, and don't hvae to care for much more. You can then work with content providers to read and store data.
http://developer.android.com/guide/topics/providers/content-providers.html
2) SharedPreferences. If you just intend to store like the last, or the last 3 Operations, you can just use shared Preferences. This is way less overhead than adding a Database, if it is a small project, albeit you will have to keep your data structured yourself.
http://developer.android.com/reference/android/content/SharedPreferences.html
3) If you just want to store the users current session you can just Keep a Stack of the used operations. On undo, or however you call it, you would just pop the stack.
By implementing onSaveInstanceState and Parcelable you can make sure that no data is lost on rotation / low memory and such.
I personally would advise you without knowing more about your project to use plain java objects and storing the state. A calculater would in most cases not need persistent storage. If you really want to know what the user did 2 weeks ago, you should use a Database.
I would recommend you to use database(SQLite) for storing the data.
If you don't know more about SQLite in android have a look at these
tutorials.
I think database should be handy for history if more than one operations has to be stored else for one operation you can use shared preference.
I am writing an app that contains a list of items with their information. When the users open the app, they would see the list, and when they select a particular item, they would get all the information about that item.
Right now my approach is I store the data in a multidimensional array in the java source file. When I push a new update, I might add new items in the java source file (so the array gets bigger). I wonder if this is the best approach. I tried looking up relevant information about array and database on the Internet, but I can't seem to find the information I need. Any advice for me?
Also, if in the future, I create a function for users to add their own items to the list, what's the impact?
If the user should be able to update it, if you should be able to update it dynamically (for instance update from internet), then the database is a must.
If that data is static and won't change unless you update the app, you can store it in the code or better, in a file (you can store in JSON format for ease of reading & parsing)
If use array, the newly added items by the user will be gone when the app restarts.
You can use a SQLite database stored directly on your phone (sd card for example).
An analysis of the storage options and what they're commonly used for can be found here. Personally I suggest SQLite database.
The database is good for permanent data, but realize it can create a bottleneck if writing to disk is not really necessary.
I am in the middle of refactoring an App to no longer use SQLite. I was inserting large amounts(10,000-100,000) of new rows at once, and it looked like it was going to take an hour based on my log feedback of the status. When I keep it in memory, it all loaded in about 5 seconds.
I want to write an android app that basically shows titles of songs in a list view, and by selecting one song in the list, the lyrics of that sing are shown in a textview. In a second step there would be maybe a translation of the lyrics for every song.
So far nothing too complicated, but since I'm completely new to android programming, I wonder what's the best approach to store the data (i.e. the song titles/lyrics). I thought about a single (xml?)-file for each song where the filename is the title and the file contains the lyrics. I think that would make it easy to add new songs. where would such files be stored typically? /res/xml?
Or would another approach be more suitable? database storage? content provider?
I would use a database and a content provider since it becomes very trivial to bind the content to ListViews using the Loader API.
According to the documentation about Data Storage, you have several options.
But the storage strategy that seems to meet your needs is an SQLite Database.
You can make use of SQLiteOpenHelper and ContentProvider to get it working.
It will require quite an amount of work and understanding to implement it but it's worth the effort :
Esier than other solutions to manipulate data once it stored.
A lot of android component are designed to work with databases cursors.
Easy to add new data and structures as your application evolves.
You will learn a lot about how develop android apps "the right way".
If i were you i would avoid :
SharedPreferences because this is not suited for list of data as there isn't any "id mechanism". On the other hand it is pretty easy to use for small settings.
Internal Storage because it is difficult to manipulate the data once it is stored. Also it is not the recommended way to store that type of data.