saving data on tabs - android

I have 3 tabs, each a separate activity. I want to save data when user clicks save on either of the tabs. There are couple of options available; shared preference, global variables or saving the objects in context.
EDIT:I have to save an image & textfield
Android Shared Preferences
Store Objects in ApplicationContext
Any suggestions on which method to pick ?
thanks

That is entirely dependent upon the length of time you wish to store them for.
Storing in the Application Context will not persist data after the application has ended. It just stores in memory.
Shared preferences is a possibility for your string data but image data would require some manipulation and may have other restrictions for example size of data (TBC).
Please consult the relevant SDK documentation on Data Storage
The most convenient way would be to save the text in shared preferences and the image to disk. Refer to the getDir() function if you want to store in the application directory

Related

How to save user-entered data for next time app is opened?

I have multiple views that user-entered data can be stored in. I also have views that don't do anything except for decoration, like spaces and dividers.
How can I save everything on screen so that when the app closes and I go back in everything is as I left it?
There are many ways to persist data on Android-
File Storage (Internal / External) : Ideal for storing big files(Photo / video etc)
Shared Preferences : Ideal for storing text values(Configuration values)
Database : Ideal for storing structured data (List of items etc)
For your use case, check out shared preferences.
Learn more about it from the official android guide
If you want users data to be persisted use below:
1. Shared preference if data is less
2. Room database to store large data set

Saving huge arraylist, index, and other primitives

I have a quiz app that shuffles everytime it is run. What is the most elegant way of saving a game state locally so that my user can resume the quiz using the same ordered list as before, as well as other primitives such as score, level, life points, etc.
I already have a bundle that saves this state when the game is paused. How can I retain the state when my activity is destroyed.
I know of shared preferences, but there must be something more elegant?
I think we need to understand firstly that there is several possibilities to save data in your app (with possibility to re-enter application and restore data):
1) Save on disc (it fast, but you can erase this data, for example, when reinstall application)
2) Save on remote disc (it slow, but it will keep your game progress throug removing and reinstalling app).
So first of all you need to choose one of this possibilities. I think in your case it will be enough to keep changes locally on disc (besides remote storage cost some additional $$)
In first possibility there are several variants to implement it:
- through shared preferences
- through saving in file (internal storage)
- through saving in file (external storage)
- through keeping data in database
I think last variant is not for you, since you want to just save state of game.
And 'external storage' variant is not for you, too, because game state is private data of your application.
So you need to choose between shared preferences and internal storrage.
In this dilemma, you can think about what you like more. For me shared preferences provides convenient and async way to save data, which is more preferable in game. But you need to think about how many data you want to keep.
If it's not big, choose Shared preferences. And if you do enance your way to save in SharedPreferences (write less code and make it more readable), you can import Gson or Jackson library. Then pick out special GameState class for your game and just convert it to json string and save. Same for restoring. Pick string from prefs and parse it to GameState. That's all.
Hope it helps. :)
I suggest you to use a database and a cursor for retrieving the huge data list. https://developer.android.com/training/basics/data-storage/databases.html
SQLite is very fast and not hard to implement.

Use database for session management

Can I use a column in table to check if user has logged in.
When user logs in I set value to 1. When he logs out I set to 0.I dont want to use SharedPreferences. Is using it like this inefficient.
I used sharedpreferences first. I set username in the sharedpreference along with other preferences and display username in the nav drawer. When I install the app in another device, I think even the sharedpreference file will be installed and I saw that in the new device the username is displayed in the from the sharedpreference file though that user doesn't exist in the table in that device's database
When user logs in I set value to 1. When he logs out I set to 0
Here, what will happen is when you set values to 0 or 1 you have to open and close database everytime and will be a hastle to maintain. you have to check it each time what flag is. while in SharedPreferences it will be globally accesible and easy to set flag.
I would suggest you to use SharedPreferences because
SharedPreferences is a key/value store where you can save a data under certain key. To read the data from the store you have to know the key of the data. This makes reading the data very easy. But as easy as it is to store a small amount of data as difficult it is to store and read large structured data as you need to define key for every single data, furthermore you cannot really search within the data except you have a certain concept for naming the keys.
To give an example, SharedPreferences are useful for storing user preferences, where there are just a handful of variables that need storing. SQLite on the other hand would be better for storing data where there is a large set of items, such as song titles in a music library which need to be searched through
Check SharedPreferences and SQLite.
Better to use Shared Preferences instead of using Database for such a small thing.
Refer this : http://www.androidhive.info/2012/08/android-session-management-using-shared-preferences/

How to display data that was saved in android

I am working on a project in which I am saving data in a class with a filename and data attached to a folder on the sdcard. I need another class to display a list of all the filenames and when you click on them it opens the saved data (text) in an editor or something. I am somewhat new to android, sorry if I am missing something obvious here.
So basically I need to display the file names of data that I have saved in a list view and when you click on them, it takes the data and sends it (Probably using shared prefs or BroadcastReceiver) to a class where you can edit it. I could also use a database if that would be easier.
See First way is to make variable as static so you can access by class name.
second Use file for data storage it is easy to create file.
third is use Share preference i.e.mobile seeting
fourth you can use SQlite to store data.
There are different ways to store the required data:
Shared Preference (If the data is lesser say for example login
creadentials etc).
Internal\External Storage (we can store data images,files etc .Your sd card case is of external storage)
Database (SQL Lite is present and stores structured data in a private database.)
Study the link for storage options.Choose the best options as per requirement and need.
http://developer.android.com/guide/topics/data/data-storage.html

Difference between shared preference and sqlite

I know this topic has been discussed before on Stack Overflow. But there are still some things that are not clear when I read previous posts about it. So here they are:
I know that we use shared preference for small datasets and sqlite for large data manipulation, so if we just want to save a username and password should we use shared preferences?
Won't shared preferences be lost when user uninstalls the app? For example I download an app called abc and save my username and password. Then I uninstall this app from one phone and try to access it from other phone using the same username and password. Will this be saved using shared preferences or the data be lost?
What are the main reason we use one over the other beside large and small datasets?
You can think of the difference between shared preferences and an SQLite database in terms of data size but that isn't entirely accurate. A better way to think of it is in terms of the structure of the data you want to store.
Shared preferences can only store key-value pairings whilst an SQLite database is much more flexible. So shared preferences are particularly useful for storing user preferences, e.g. should the app display notifications etc. Whilst an SQLite database is useful for just about anything.
Both data sources are local but something you should be aware of is the ability to backup your application data to cloud storage that is linked to the user's Google account. This makes it much easier for your users to change devices and for their applications to easily transfer to the new device. For more info take a look here.
In the situation you described about you will lose the user name and password in both situations. The data is stored on the phone, when you uninstall the application, the data that some with it will also be lost. The user will have to re-enter this information.
You can save the user name and pass in either the shared Preferences or a DB, that is personal preference. Just make sure you lock either down, i.e. don't share the DB or Shared Preferences that you keep this information in.
As for the difference... shared Preferences should hold well... shared Preferences... here is an example:
If I create an option to change the background color, I will store all available options in a DB that can be loaded into a adapter view for the user to choose from. But I will store the color that they have selected in the Shared Preferences. This way when the application load I can get the Shared Preference value of the background color that should be used.
SharedPreferences is used for just that, storing user preferences shared application-wide. You can use it, for example, to store a user's username, or perhaps some options he or she has configured in your app in which you want to remember.
SQLite is a relational database. It's used to store your application's data, not preferences or configuration information.
Both are stored locally on the device.
1.SharedPreferences stores only Boolean, int, float, long, String five kinds of simple data types, such as can not be conditional query. So, whether SharedPreferences data storage operation is how simple it can only be a supplement of storage, but can not completely replace other data such as the SQLite database is stored.
2.SharedPreferences based on the XML file to store key-value key used to store configuration information(mainly user preference for your application).
3.Sharedprefrece just like cookies in web which store some basic information at client side.
both store their data locally, so uninstalling the app will delete both. other than that, SharedPreferences is easier to program, and you're right about the data amounts.
In general, shared preferences should be used if you want to allow your user to directly manipulate certain data fields. Shared preferences are basically user preferences; if you would like the user to reconfigure the app to behave in different ways, you should expose that functionality as a shared preference. On the other hand, the SQLite database should be used if you want to limit the visibility of the data to just the application, if you want a stronger guarantee that the data be persistent, and if you want the application to behave independently of what is stored in the database. Of course, you can use both in one application.
Shared preferences and the database are part of local data that the application stores. If you uninstall the application, both of the data stores will be removed.

Categories

Resources