question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Preferences behaviour differs on Android

See original GitHub issue

Issue 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:closed
  • Created 5 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
obigucommented, Feb 11, 2019

The documentation of the Preferences interface could definitely improve by stating that there is no guarantee changes will be available until flush is called. The current sentence:

Changes to a preferences instance will be cached in memory until {@link #flush()} is invoked.

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.

0reactions
MrStahlfelgecommented, Jul 1, 2019

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found