sharedPreferences wont update if edited in another process - java

I have an unity activity, which launches in another process than my App:
<activity
android:name=".UnityActivity"
android:process=":UnityKillsMe"/>
I save my SharedPreferences encrypted with this question (just in case this affects my problem and I have no idea).
Now if I want to edit a sharedPreference in process=":UnityKillsMe", the edits are not beeing accessable in mainProcess, it just takes the old version of the SharedPreferences. Not until I kill the mainProcess and launch the app again. If I do not kill the mainProcess, and edit the SharedPreferences, while they have been edited in process=":UnityKillsMe", the edits will be overwritten, and are lost.
How do I refresh SharedPreferences in the main process ?

The current implementation of SharedPreferences in Android is not process-safe.
From the docs:
Note: currently this class does not support use across multiple
processes. This will be added later.
The question is do you really need multiple processes in your application?
If you really do, i would suggest you to take a look at Tray, or consider some other form of persistent storage, such as a database.
Check out this answer also.

Related

Save activity state Android studio

How could i save the activity state and values after closing application and when i open it again it have the values of radio button and spinner and edit text
in android studio in kotlin or in java
You could save your data in SQLite database before closing your application and then retrieving again when the activity is started. Checkout this guide on how to use Room to save data. I also recommend you to read about other types of storage in this guide
You could use SharedPreferences.
SharedPreferences can be used as some sort of cache/cookie which will be useful when the user's detail/action is to be remembered when he comes back to use the application.
Android provides many ways of storing data of an application. One of this way is called Shared Preferences. Shared Preferences allow you to save and retrieve data in the form of key,value pair.

Android session handling

How do I manage a session in an Android app after the user successfully logs in? Should I use SharedPreferences or static variables to store the session info?
The user must log in every time the app launches too.
Try to use Account Manager. It was designed for it. Here is a tutorial for it.
Anyway it is not recomended to use preferences for it, because at the end it is plain XML file, so anyone can steal data from it. Also it is not recomended to use static variables, because you never know when your process will be killed. For example somebody could call to your phone and android could kill your app and restart it after call ends.

How to make your app save automaticly?

I am working on an App in android studio. The only thing is that if you close the app you need to start over with the game. Do you guys know how i can integrate a autosave option in my app that every time the user closes the app and start it up again that they don't need to start over?
onSavedInstanceState is used when recreating an activity due to things like orientation changes. check this link: http://developer.android.com/training/basics/activity-lifecycle/recreating.html
If the user is exiting the application completely then you should look into persisting the data with a sqlite db or using shared preferences during the activity lifecycle, check this link: http://www.herongyang.com/Android/Android-Application-Activity-Lifecycle.jpg
When the user starts the app again then check the persisted data during onCreate()

Overwrite app and deletes database on Android

I am creating multiple apps and every app has same package so if I install one then it should overwrite the other, i.e. one of these apps can only be installed in the phone. This may sound weird but this is the way apps need to be. I have all the apps ready but I have this issue that I want to drop the table of database on every install, so if I install second app it overwrites the first one but the database is not deleted. I cannot change the VersionCode as I won't be able to overwrite with the older version so all apps have same package and same version code.
Basically it should ask for a password on every first run of the app, then I create a table and store it there that this user is authenticated, so for every new app this has to be done again but on update the table remains there which results in no prompt for password. Now how do i detect and drop the table from database. Even if I use sharedpreferences instead of database, they remain there on the update and I can't even detect if the app is updated as version code is same.
I will really appreciate if anyone can give me a solution or a hint.
Thank you,
Hamza Manzoor
You could store a seperate version value of the apps in the SharedPreferences and check the value in it against a list or numerical value and decide depending on the result.
You could also use Android's built-in version (not the nummrical value but the string value).
If you go for the SharedPreferences solution, you can use a kind of algorithm for the version value. Let's say one app is numbered "A", another "B" and yet another "C". You then append the app's real version to that, i.e. "A1.0" or "B2.4", ... (you get the idea). This way Android has no idea of what app is curretly being installed.
Whenever you start an app just check for the first value. If that is different from the app's normal value you know, it has been replaced. Otherwise check for the version value and see, whether you need to update the database or not.
// May I add:
Of course this is a possible source for bugs but if you are careful this should work. If someone has a better solution, please post.
Your app will not be overwritten\upgraded via Google Play mechanism in case they will have the same versionCode. Only via "adb install -rd" or some root-enabled side-loader.
IMHO, the right way to act is
increase versionCode and DB SCHEMA_VERSION on each upgrade as usual
in your DB class (which extends SQLiteOpenHelper) in onUpgrade() method you should check DB version and set some variable in Prefs accordingly, later in Activity you'll be able to fire a password dialog (open DB on onCreate(), check prefs on onResume()).

Methods to be performed when app 1st installs android

Hi guys I have been looking around for the answer to this but have come up with nothing, I would like to have an activity that asks the users for their details and then send them details to an external database after they first install the app. Does anyone have any suggestions on how to create an activity that will only be used when the app is launched for the 1st time. Cheers to anyone who can help
Save a flag in shared preferences that you set after completing the activity. You can actually save this flag anywhere, as long as you have it in some sort of persistent storage. Then on load, check in that activity if the flag exists (you can do this in onCreate), if it does, launch an intent to another activity.
Create a file or store a persistent property at the first boot.
Check for the existence of that file/property at every boot.

Categories

Resources