Shared preferences in Native vs Flutter - android

My flutter app has a native component to it. This native component is using platform.invokeMethod. In this native component I save a few things to shared prefs using the code
sharedPreferences = getSharedPreferences("foo", Context.MODE_PRIVATE)
sharedPrerences.putString(...
Now, I want to read from this shared preference file from my flutter / dart code. But the API does not have a way for me to specify the shared preference file name (foo). I can only do this
SharedPreference.getInstance().getString(..
How do can I solve this?

I'm assuming you are using the Shared Preferences-plugin on the Flutter side. That only accesses a single file whose name is hard-coded in the plugin.
Instead of saving the preferences in the native android side, you could instead return them from the invokeMethod to Flutter, and then use the plugin to save them.
And - although not the most elegant solution - you could write the preferences on the android side into the same file the plugin is using: "FlutterSharedPreferences". This would probably involve also calling reload on the flutter side after the write to make the plugin refresh its local values.

Related

persistent preferences in android

I want to create a preference set which the user will not easily be able to delete/clean...
the idea i got is to create the persistence files in the public folder as and hidden folder (this way the files wont be deleted on unnistall or if the user asks for clean apps data)
my questions:
1- does anyone knows a better way? is it possible to do that using the apps private folder and somehow tag the files for no deletion?
2- is possible to use native android preferences framework and specify another file to store the properties? how?
3- does anyone know a good, SMALL and simple to use API for persistent properties similar to androids default?

Deploy settings during 1st install Android App

I'm developping an app that contains some settings like server ip, sharedpreference filename and other that I would prefer save into a single file and not hard coded somewhere in the code.
So I would like to know what are the best practices nowadays concerning the deployement of these kind of settings. More, how to access it everywhere in the app ?
The android sharedpreference allows me to save it after the 1st launch but I don't know how to deploy it with the App.
regards,
A common way to deploy arbitrary data is the use of raw resources.
Create a file with your settings and save it to the raw resource folder. When your app is run for the first time, you can create your SharedPreference instance and copy the values from the raw resource to the preferences.
One option is to create a Configuration.java class and put the configuration there. You can have different build flavors for different build types (development, production etc.)
Another option would be to use the class generated by Gradle: BuildConfig.class, where you can put some configuration data from your build.gradle script.
EDIT:
Shared Preferences shouldn't be used for that if the server IP doesn't change during the lifetime of the application.

Usage of Shared Preferences in android library projects

Usage of Shared Preferences in android library projects is allowed?I am trying to work on rating bar but i am not sure whether i can use Shared preferences to write that.
If you have a Context, supplied to you by the app that is using your library, you can use SharedPreferences.
However, you need to be a bit careful to ensure that you do not accidentally try using the same keys as the app might use. You might consider using a unique SharedPreferences file, rather than getDefaultSharedPreferences().

Is it possible to access App's version from library project in Android?

I've a android library which is called from an App. I want to access App's version from library project. Is it possible at all?
EDIT: I don't control the calling app. I need some way to get calling app's version from library.
If you pass the context information, you should be able to use
PacketManager yourPacket = context.getPackageManager();
which gives you oodles of information.
One option you have to is get the apps version from your own app itself, and then make it available "manually" to the library by storing it in some sort of shared storage like a WORLD_READABLE file, or on the SDcard or something.
Or perhaps depending on the library, and how much you are able to modify it you could also use some sort of Static object to hold the data for you.

Can we make sure that our app uses our version of SQLITE instead of the one which is provided by android.. by using Android NDK?

our application is having problems working with default sqlite version provided in Android 2.1.
So we want to use the customized sqlite version for our App.
So my question is .. if we get the latest sqlite version from SQLITE.org and compile with android NDK, will we be able to invoke the our sqlite library when ever there are database operations happening in our application?
ex: if there is a call like "db.execSQL()" in Application code, How are we sure that it is gonna call the custom library?
In addition to building your own sqlite shared object, you will need to replace one or more of the standard java sources from Android with your own (unless you actually just replace the platform's sqlite shared object). The platform framework loads /system/lib/libsqlite.so automatically.
I would suggest not naming your shared object libsqlite.so. I've seen Android's dlopen() being told to load two shared objects with different paths but the same name, only to have the first one opened twice.

Categories

Resources