Win a copy of Getting started with Java on the Raspberry Pi this week in the Raspberry Pi forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Liutauras Vilda
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Piet Souris

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

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Im following the steps laid out https://developer.android.com/codelabs/android-training-adding-settings-to-app?index=..%2F..%2Fandroid-training#3 for using the Settings Activity Template and despite some changes (e.g. now a 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:



MainActivity.java:



root_preferences.xml



strings.xml:



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.

 
Ranch Hand
Posts: 605
11
Android Python Open BSD VI Editor Slackware
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Are you running the app in an emulator or on a device? in both cases, please uninstall and launch it again pressing the run icon. The FIRST time you should see `-1`;
after the first time you should see a value and this value will be "persistent" because you use SharedPreferences, I guess this is the reason why you are seeing US not the xml default value you mention.

In the case you get still `US` and so your reasoning is right , uninstall it again and change the default value to `NL` if the content is NL this means that this string get passed into the SharedPreferences from the first attempt, and so the tutorial  may be wrong, it happens several times even with the official documentation , they gotta produce a lot of codelabs and guides on weekly basis, few developers notice small imperfections
 
Weyard Wiz
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Giovanni Montano wrote:
Are you running the app in an emulator or on a device? in both cases, please uninstall and launch it again pressing the run icon. The FIRST time you should see `-1`;
after the first time you should see a value and this value will be "persistent" because you use SharedPreferences, I guess this is the reason why you are seeing US not the xml default value you mention.

In the case you get still `US` and so your reasoning is right , uninstall it again and change the default value to `NL` if the content is NL this means that this string get passed into the SharedPreferences from the first attempt, and so the tutorial  may be wrong, it happens several times even with the official documentation , they gotta produce a lot of codelabs and guides on weekly basis, few developers notice small imperfections



Thanks Giovanni

Im testing this on pixel6 emulator. Ive tried all those steps you mentioned before i posted here and it was still displaying US. I still dont know why, but I went ahead and recreated the project and this time I checked the subscreens settings when creating a settings activity template to create various settings screens instead of the root_preferences screen.
This time I see -1 displayed the first time so its working this way now.
 
Giovanni Montano
Ranch Hand
Posts: 605
11
Android Python Open BSD VI Editor Slackware
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Weyard Wiz wrote:

Giovanni Montano wrote:
This time I see -1 displayed the first time so its working this way now.



glad to see it helped, I guess the changes suggested worked, just  android studio lately has some cache problems, so changes are not always picked up immediately, at this regard i suggest you to often to seek for  the IDE option`invalidate cache and restart`, you showed some persistence and logical reasoning that could land you to become  a senior in few years.

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I'm sorry to hear that you're having difficulty with your code. Can you provide a little more detail on what you're trying to do? We might be able to offer some help. You can reach us at  https://andersenlab.com/services/mobile-development. Thank you!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic