Android onSaveInstanceState for all instances of activity - android

After reading the documention it seems that onSaveInstanceStaate works per instance (as the name suggests). I am just wondering what the preffered method of storing data is so that it is available for all instances of that activity?

As MaciejGorski mentioned in his comment, there are different levels of data storage available in Android:
Shared preferences
Internal storage
External storage
SQLite database
Network
From personal experience, the lower you go down this list, the more complicated your implementation will become. Thus, if you are simply trying to save simple data for your app to be shared among different instances of an activity (or of multiple activities), shared preferences are certainly the way to go. You can even create private shared preferences, which only your app can access.
In any case, check out this SO answer for how to implement them: How to use SharedPreferences in Android to store, fetch and edit values

Related

Multiple SharedPreferences

Actually, I have a doubt regarding SharedPreferences in android.
I have started learning Android a few days back and I am creating a SignUp Page for now, for that, I have searched the internet and got some ideas and now I am using multiple shared preferences in my code which I am thinking it would be a bad practice. So, I just wanna know, what happens to the Application if we create multiple shared preferences in the code.
This will just result in multiple SharedPrefenrences files (XML) in the data folder of your app. This is neither a problem nor a bad practice. If you have larger sets of structured data, consider using a database (e.g. SQLite/Room).
It is not bad practice at all. There is always a default shared preference.
we can get default shared pref filename using .getDefaultSharedPreferences() method.
you can get back up of any single shared pref file which is efficient.
With the concept of consuming more memory we shouldn't use SharedPreference to store large amounts of data, Alwayas use SQL DB in android for that. Multiple sharedpreference is good because , you can store data seperate for different sections within the app if it doesn't need to be shared. Shared preference is just a xml file with key value pair. So if you store only simple key value pairs, its okay to have multiple shared preferences. But be logical in your decision, dont just do that because you can

Can I use two different data storage in one project?

I want to use sharedpreferences in my login form, but i also want keep XAMPP as my main storage. What is the best data storage when I want to do my app in offline mode?
Yes, you're allowed to mix and match any type of data storage you wish because none of them were designed to be in conflict with each other.
However, it's a good idea to understand what type of data storages is available for you to use, because different types of storages has different benefits and drawbacks.
SharedPreferences is a great way to store a user's preferences, or simple data with a single value. It's great for storing a user's preferences because when creating Settings, it's extremely common to use Preferences which directly reflects the values within your SharedPreferences. However, due to it's design, SharedPreferences isn't a good idea to store large amounts of data or dynamically created data.
For that, it's better to use a database and you're free to use any type of database you wish. But for offline mode, it's best to use the SQLite Database that's offered by Android by default.
However, if you do want to use XAMPP, it's not uncommon to see developers who store their full data in an online database, but cache a few data within a local database like SQlite.
Therefore, there's zero conflicts among data storage options, so mix and match to what benefits your app's design best.
If you want to store data in locally then you can use SQLite Database and yeah XAMPP is whole different thing to store data for this you need to create APIs to communicate with database.
You can also use Sharedpreferences for storing the data but Sharedpreferences will store data with the key-value pair only.
But you can use both for different different purposes for the same app

Is Shared Preferences safe for private datas?

Im saving datas from my db/user into a gson formated ArrayList in SharedPreferences. Now my question :
Is it safe to save these datas (or data in general) into Sharedpreferences. Are users able to read these gson Arraylists out ? Maybe from SD card ,in a folder or somewhere else.
Thank you !
They are stored as xml files in your app directory, with permissions that allow only your app to access them. But on rooted device they are easily accessible. If you are concerned with security then you may use encryption, those projects might be usefull to you:
https://github.com/rtoshiro/SecureSharedPreferences
https://github.com/sveinungkb/encrypted-userprefs
still those projects does not give you 100% guarantee, hacker may decompile your apk and find keys used to encrypt shared preferences. So if your data is of use only for short time then remember to remove it from your device once user has finished using it. You may for example keep data on server and download it only when needed, caching locally only for short time - when its needed.
SharedPreferences is just a file located in phone private memory. So user can't access it but root can. Root can everything and many users have root's nowadays. You shouldn't store fragile data there
Android SharedPreference security
You can read all shared preferences Data
The SharedPreferences class provides a general framework that allows
you to save and retrieve persistent key-value pairs of primitive data
types.
To see the information in the store you need to know the important thing from the data. This will make reading through the information super easy. But as simple as it's to keep a tiny bit of data as difficult it's to keep and browse large structured data since you need to define key for every data, in addition you can't really search inside the data except you've got a certain concept for naming the secrets.
Please read Android SharedPreference security

Difference between "extending Application" and sharedPreferences?

I am looking to store simple variables so that if the app process is stopped the data will still be available when it relaunches. I looked into extending the Application class and sharedPreferences. To me, it seems that extending Application is good for temporary global variables that are deleted when the app process is stopped. sharedPreferences however saves the variable onto a file so that is is always available. What are the differences between the two, and what are their optimal uses? Also, what is the best for storing variables that you want to keep even if the app is stopped?
When your app's process is killed, any data stored in the Application class will be lost. You should only use that for storing data that is not needed across multiple launches and uses of your app.
For storing simple data like highscores, sound preference, showing a dialog at startup preference, your best bet would be SharedPreferences. For more comprehensive data, like a list of purchases made by an user in your app, or notes in a todo list app, you should use an SQLite Database.
Beyond that, if you want to store files like PDFs etc. or images (images can also be saved into a database), you can use the internal storage or the external storage (may be an SD Card, or a partition on the internal storage). Please keep in mind that on most devices, internal storage is very limited and you shouldn't save excessively large file there.
This part of the documentation should help you with storage options.

how to cache Google API key string to memory?

I'm developing an Android app that uses Google Places API.
In order to keep the Google API Key out of the application code, I fetch it from my own authenticated API server and its needed in many places around the app.
what is the best way of storing / caching it to memory ?
I understand that just using a singleton will be garbage collected.
is extending the Application class and adding methods to there a good method ?
anything else I might consider ?
Use SharedPreferences
To save to preferences:
PreferenceManager.getDefaultSharedPreferences(context).edit().putString("MYAPIKEY",
"myStringToSave").commit();
To get a stored preference:
PreferenceManager.getDefaultSharedPreferences(context).getString("MYAPIKEY",
"defaultStringIfNothingFound");
Where context is your Context.
Once you get it from server, save it. You can then access it as and when you want. Uninstalling an app will of course remove its contents. Else it is persistant.
A VM cannot garbage collect string references that classes can still reach, so I don't see why you cannot keep the string in a singleton and store it as a static reference. You can also store it in a shared preference like Doomsknight suggests, but this will not be cached in memory. Shared preferences are stored as XML files on a persistence store (against values available on a RAM). Values are retrieved back from them.
Perhaps you need a combination of the two. Store the value in a shared preference and then cache it in memory when your app starts. I'm unaware if the PreferenceManager by itself has a caching mechanism of some sort in memory.

Categories

Resources