Aller au contenu
View in the app

A better way to browse. Learn more.

Forum de Frandroid

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

getDefaultSharedPreferences ne marche pas

Featured Replies

Posté(e)

PROBLEME RESOLU /O/ (Voir Edit)

Bonjour ! J'ouvre ce topic car j'ai un problème avec la fonction getDefaultSharedPreferences, en effet elle ne me retourne pas la valeur de la clé demandée.

Code de la Main activity qui bloque

[...]
public class MainActivity extends Activity{
public String messageTraduit ="";
public int changeModeInt=2;
public String theme = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
theme = prefs.getString("THEME_APP", null);

if(theme=="sombre"){
setTheme(android.R.style.Theme_Holo);
}
else if(theme=="clair"){
setTheme(android.R.style.Theme_Holo_Light);
}
[...]

Mes préfèrences

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:title="Apparence">
<ListPreference
android:key="THEME_APP"
android:dialogTitle="Choisissez un thême"
android:entries="@array/liste_theme"
android:entryValues="@array/liste_theme_valeur"
android:title="Choisir un thême" />
</PreferenceCategory>
</PreferenceScreen>

Et enfin mon array


<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="liste_theme">
<item>Clair</item>
<item>Clair et bar d\'action sombre</item>
<item>Sombre</item>
</array>
<array name="liste_theme_valeur">
<item>clair</item>
<item>clair_sombre</item>
<item>sombre</item>
</array>
</resources>

J'essaye via cette clé de changer le thême de mon app mais il ne change pas du tout, est-ce que j'utilise mal la méthode getString(Strin key, Default); ?

Je sais que le problème vient de là car si j'indique comme valeur pour le theme:

theme = "sombre"
//Au lieu de
theme = prefs.getString("THEME_APP", null);

Le thème change bien en Holo.

Merci d'avance \o/

EDIT:

En fait le problème vient du fait que pour comparer ma variable theme avec la couleur j'utilisais "==", alors qu'en Java il faut utiliser .equals(String string), Voilà o/

Modifié par mac-ak47

Archivé

Ce sujet est désormais archivé et ne peut plus recevoir de nouvelles réponses.

Account

Navigation

Rechercher

Rechercher

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.