Why am I getting the default value the first time running the app?

Issue

I’m following the steps laid out here for using the Settings Activity Template and despite some changes (e.g. now a root_preferences.xml file gets created instead of multiple XML files for different preference screens when selecting the Settings Activity template) I pretty much have nailed down the steps very similarly.

It says towards the end:

The first time you run the app, you should see "-1" displayed in the
Toast because you haven’t changed the setting yet.

However, instead of -1, I’m seeing US as the toast message. I’m thinking that’s because it’s the default value, but they also set the default value as the US, yet the expectation according to the instructions is -1 the first time they run the app…how come?

Here is my code and the result I’m getting first time running the app:

SettingsActivity.java:

package com.example.droidcafe;

import android.os.Bundle;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.PreferenceFragmentCompat;

public class SettingsActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings_activity);
        if (savedInstanceState == null) {
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.settings, new SettingsFragment())
                    .commit();
        }
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    }

    public static class SettingsFragment extends PreferenceFragmentCompat {
        @Override
        public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
            setPreferencesFromResource(R.xml.root_preferences, rootKey);
        }
    }
}

MainActivity.java:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent =
                    new Intent(MainActivity.this, OrderActivity.class);
            intent.putExtra(EXTRA_MESSAGE, mOrderMessage);
            startActivity(intent);
        }

    });

    PreferenceManager.setDefaultValues(this,
            R.xml.root_preferences, false);

    SharedPreferences sharedPref = PreferenceManager
            .getDefaultSharedPreferences(this);
    String marketPref = sharedPref
            .getString("market", "-1");
    displayToast(marketPref);

}

root_preferences.xml

<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto">

    <PreferenceCategory app:title="@string/pref_header_account">

        <ListPreference
            app:defaultValue="US"
            app:entries="@array/pref_market_titles"
            app:entryValues="@array/pref_market_values"
            app:key="market"
            app:negativeButtonText="@null"
            app:positiveButtonText="@null"
            app:title="@string/pref_title_account" />

    </PreferenceCategory>

</PreferenceScreen>

strings.xml:

<string name="pref_header_account">Account</string>
<!-- Sync Preferences -->
<string name="pref_title_account">Market</string>
<string-array name="pref_market_titles">
    <item>United States</item>
    <item>Canada</item>
    <item>United Kingdom</item>
    <item>India</item>
    <item>Japan</item>
    <item>Other</item>
</string-array>

<string-array name="pref_market_values">
    <item>US</item>
    <item>CA</item>
    <item>UK</item>
    <item>IN</item>
    <item>JA0</item>
    <item>-1</item>
</string-array>

First time running the app:

US

Expectation: -1

I realize the default value is US, but their codelab default is also US, yet the codelab says it should display first time -1.

default

Solution

I still don’t know why, but I went ahead and recreated the project and this time I checked the sub-screens settings when creating a settings activity template to create various settings screens instead of the root_preferences screen.

subscreens

This time I see -1 displayed the first time so it’s working this way now.

Answered By – Cataster

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published