I've got an activity and I want to keep everything inside it (Bitmaps, texts, buttons..) so when the user restarts the phone and clicks on my app, everything is like the last time he used it.
Any idea?
I've been reading about the lifecycle of an application in Android, and i know the basics theoretically but I don't know how to implement it.
Thank you.
Actually, you don't need to do anything on objects such as Buttons and TextViews, since they do not change during your Activity's lifetime. When your application restarts, they just get recreated and they look the same anytime. Things that you want to save are possibly user inputs or downloaded files. If you have any of these, then I'd recommend learning this post, it describes different data storage mechanisms in Android. For different kinds of data you'll need different solutions, for example text inputs can be saved in SharedPreferences, but bitmaps should probably be saved in the file system. Hope this helps.
Maybe it is not the best solution, but you can try SharedPreferences and save different user data into different variables upon exiting.
When the app is restarted, read the values stored in the SharedPreferences and display these values in your respective views.
Other than SharedPreferences, you can also try out these various options according to the type of data you would want to persist!
Related
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.
If I was creating an Android app that for example stored alarm clock times and information, would it be best to put that information in a database, write to file, or user preferences. I would only access that data to create the list of alarms and when editing a specific alarm.
Personally, the overhead of creating a database is overkill for this. Writing to a file, well, it depends, but it seems like this would be a good way to do it. I'm assuming that you don't know how many of them there will be. If it's a fixed small number, you could use preferences.
I tend to pick the easiest way that doesn't rely on anything else working correctly :)
I am making an application which is saving the values in arrays and I have 7 arrays like this. Now I want to save these values for future use although the application is restarted. I am confused between database and sharedprefrences. Pls tell me what I can use for this approach and why??
And if I use sharedpreferences then is there any way to store multiple rows of data or I can use SQLite only?? Please do tell.. Thanx in advance...
These two are 2 of the methods for saving persistent data. There are both pros and cons.
My opinion is that if you want to save a large amount of data I will go with the database. The preferences are more like saving states or valuable information that require a key-value pair. A simple example is saving the value of an EditText or how many times the user has logged in the application.
Although in your case the database might seem more appropriate, the implementation is a little time consuming and I don't know if it's worth it for your case. If there is a possibility of expanding the dataset that you wish to save then go with the database. It's up to you.
I hope this answer gave you a briefly overview.
One way is discussed here:
StackOverflow - How to Implement Persistent Storage in Android
Here's something that shows three different kinds of storage: Data Storage in Android
I'm developing a quiz game and I want to save the progress... Actually I use sharedsettings, is easy and fast, but the user can delete program files and lose the progress. Same happens with a database. I should use a file? I'm only interesting to save a boolean for each question: "correct answered or not?"
Thank you!!
SharedSettings. And you're already using it. It's very difficult to save a user from his/herself. If it's on the device then it can be destroyed either stupidly or maliciously. And don't forget that everything is a file and there's nothing you can put on the device that will be hack/idiot proof.
The only way to get around this would be to store user settings off the device via something like a web service (and backing database.) But I would say that if you're using SharedSettings... you're OK. Leave it at that. I would only consider using an app-specific db if you expect the number of question you track to grow to a very large size and want more control over the data.
Where in code logic?
In general, it's best to save any UI preferences at onPause. Android design wants you to never have a "save" button, rather save constantly.
As far as at each question, i guess you would save after answering each question?
Some of the users of my application complain that sometimes (in a random way) the settings of my application are getting reverted to their default state (usually after a reboot of the phone). I never managed to reproduce the problem though. I am thinking it is due to the fact that in many places in my app I have a piece of code that calls the shared preferences editor and commits changes - Can it resolves in corrupting the shared preference file if I try to commit several changes to the same preference file at the same time? (Multi-thread application)
I am really lost. I tried to look in the web for hours to find a solution without a success.
If anyone has even an idea so I can start investigating, I would be grateful.
Thanks,
Amit Moran
I'd echo the other answers - that you need to avoid conflicts if you don't want to corrupt the file - and I'd go further to suggest that you're probably misuing SharedPreferences.
SPs are designed to store small pieces of information about your app - user settings like volume or whether music is playing or things like that.
SPs are NOT designed for storing data which changes often and/or large amounts of data and it's a bad idea to try to do this (for the reasons you've discovered and a few others).
Remember that SPs are really just an XML file - you're incurring the overhead of parsing and recreating that every time you change it too!
The idea of an App which updates SPs in more than one thread is a bit mad I think - you need a better way of managing and storing the data you're saving - it will pay-off for you in more than one way...
According to the SharedPreferences.Editor documentation:
Note that when two editors are modifying preferences at the same time, the last one to call commit wins.
From this I gather that multiple simultaneous commits will not wipe out your preferences, but it's possible that not all changes you are attempting to write will end up getting written if multiple Editor instances are being used simultaneously. To avoid this you could put all preference modifications in synchronized blocks or even use one synchronized static method for all preference writing.
I suggest you use a singleton to manage the preferences. Whether you do this by implementing a true java singleton, or by using Android's Application Context is up to you. (see this question for several good arguments for/against each)
For something like SharedPreferences, this is a good way to manage them especially for a multi-thread application. This will possibly eliminate some of the questioning of whether or not the commits are conflicting with each other. This may not be the whole problem, but it's somewhere to start.
I had a similar problem: my preferences had not been saved reliably. On some devices (in my case the XOOM-Tablet) data got sometimes lost and sometimes not. I've solved the problem by simply calling clear() on the editor before commiting new data.
Shared Preferences get lost after shutting down device or killing the app
I found I was losing a particular entry in SharedPreferences by opening the editor, doing a getString on it, and then committing without doing a putString on the entry first, even if there was no required change. Once I stubbed in a putString to save the value no matter what, the entry stopped vanishing after the commit.