Preferences behaviour differs on Android
See original GitHub issueIssue details
I notice that behaviour of Preferences
is different on Android; a newly-written preference will not be readable until a flush has occurred. On other platforms, it can be read immediately, provided the same Preferences
instance is used.
This is because of the use of the SharedPreferences Editor in the Android implementation. I am happy to make the change myself to bring the behaviour in-line with the other platforms; the reason this is an issue rather than a PR is that I would like input from contributors as to which behaviour is actually desired.
Minimal example
import com.badlogic.gdx.*;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.graphics.GL20;
public class Barebones extends ApplicationAdapter {
public void create () {
Preferences preferences = Gdx.app.getPreferences(".pref");
preferences.putString("flower", "tulip");
Gdx.app.log("Flower", preferences.getString("flower", "daffodil"));
// On Desktop and iOS this prints "tulip". On Android it prints "daffodil".
preferences.flush();
Gdx.app.log("Flower", preferences.getString("flower", "daffodil"));
// This will now print "tulip" on all platforms.
}
public void render () {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
}
public static void main (String[] args) throws Exception {
new LwjglApplication(new Barebones());
}
}
Version of LibGDX and/or relevant dependencies
Latest
Please select the affected platforms
- Android
- iOS (robovm)
- iOS (MOE)
- HTML/GWT
- Windows
- Linux
- MacOS
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (6 by maintainers)
Top Results From Across the Web
Behavior changes: all apps - Android Developers
The Android 12 platform includes behavior changes that may affect your app. The following behavior changes apply to all apps when they run...
Read more >Behavior changes: Apps targeting Android 12
Like earlier releases, Android 12 includes behavior changes that may affect your app. The following behavior changes apply exclusively to apps that are ......
Read more >Preference components and attributes - Android Developers
This topic describes some of the most commonly-used Preference components and attributes used when building a settings screen.
Read more >Android 8.0 Behavior Changes
These behavior changes apply to all apps when they run on the Android 8.0 (API level 26) platform, regardless of the API level...
Read more >Behavior changes: apps targeting API 29+ - Android Developers
The behavior of the resizeableActivity manifest attribute has also changed. If an app sets resizeableActivity=false in Android 10 (API level 29) or later,...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The documentation of the
Preferences
interface could definitely improve by stating that there is no guarantee changes will be available untilflush
is called. The current sentence:is not accurate (at least it may be misleading) imo as it can be read as if changes will be available in memory until persisted to disk, which is not the case on the Android implementation.
When doing this change, tt should also be added that you should only use one instance of a
Preference
and hold it in your game class. Users had problems with using multiple instances on iOS.