• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

How come Im getting the default value the first time running the app?

Cataster

Lurker
May 5, 2020
2
0
Im 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, Im seeing US as the toast message. Im thinking thats because its 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 Im 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 app:



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.

 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones