Trouble with Shared Prefs.

by JoelDuggan » Sat, 09 Oct 2010 06:36:26 GMT


Sponsored Links
 Hey there all,

I'm having a problem that i don't know how to resolve.

I have a broadcast receiver catching a call from the system.  I need
this receiver to set a flag so that the next time my app runs it can
do something different.

I am trying to do this with a shared pref but it is not working.

In the receiver I do:

      SharedPreferences sharedPreferences =
context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
      sharedPreferences.edit().putBoolean("changed", true);
      sharedPreferences.edit().commit();

Then when i start my app I check like so:

         sharedPreferences = getSharedPreferences(PREFS_NAME,
MODE_PRIVATE);
         if (sharedPreferences.getBoolean("changed", false)){
            hasChanged = true;
            sharedPreferences.edit().putBoolean("changed", false);
            sharedPreferences.edit().commit();
         }


This does not work.  If I download the file from the emulator after
the receiver has triggered it is empty.  It exists but there is no key
value pair.

What am I doing wrong?  Is there a better way to flag my app to run
differently on next startup?

Any help???

--



Trouble with Shared Prefs.

by Kostya Vasilyev » Sat, 09 Oct 2010 06:46:12 GMT


 Don't call edit() multiple times.

Every call to edit() creates a new "change session", so you're changing the
value but not committing the change, and then starting a new changeset and
committing it with no new values.

--
Kostya Vasilyev --  http://kmansoft.wordpress.com 

09.10.2010 2:36  "JoelDuggan" <joeldug...@gmail.com> :

Hey there all,

I'm having a problem that i don't know how to resolve.

I have a broadcast receiver catching a call from the system.  I need
this receiver to set a flag so that the next time my app runs it can
do something different.

I am trying to do this with a shared pref but it is not working.

In the receiver I do:

     SharedPreferences sharedPreferences =
context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
     sharedPreferences.edit().putBoolean("changed", true);
     sharedPreferences.edit().commit();

Then when i start my app I check like so:

        sharedPreferences = getSharedPreferences(PREFS_NAME,
MODE_PRIVATE);
        if (sharedPreferences.getBoolean("changed", false)){
           hasChanged = true;
           sharedPreferences.edit().putBoolean("changed", false);
           sharedPreferences.edit().commit();
        }


This does not work.  If I download the file from the emulator after
the receiver has triggered it is empty.  It exists but there is no key
value pair.

What am I doing wrong?  Is there a better way to flag my app to run
differently on next startup?

Any help???

--


Sponsored Links


Trouble with Shared Prefs.

by JoelDuggan » Sat, 09 Oct 2010 22:53:29 GMT


 Thank you very much.  That makes perfect sense.  I'll give it a try
later.



>



Trouble with Shared Prefs.

by Ruchika Gupta » Mon, 11 Oct 2010 05:41:44 GMT


 Sorry for different question. How can I identify if the contact is exported
from facebook or google or both or it is local contract
from android contact book.
How can I find that? which class or variable stores information about there
source?
Appreciate any help.
Regards
Ruchika

2010/10/9 JoelDuggan <joeldug...@gmail.com>


> >



Other Threads

1. Build Source on Windows

Hi all,

As i know Android source can not be build on Windows. Cant we work that on
windows too like its on Mac. Why is this can not build on windows? what are
the barriers? if there is anyone who is aware of this issue please let me
know what we have to do to build this on windows. i would like to contribute
to make this build-able on windows...

Thank You

-- 
Thanks & Regards,
Thisara.

-- 

2. Help.. Nexus1 NewBie

Buat semua Bos-bos id-android...

Numpang tanya..

Apa aja langkah2 yg musti di"tempuh" Nexus yg baru..

saya udah ngeroot..(sukses).. custom recovery (amon-ra).. apalagi ya?

mohon bantuannya..

-- 
"Indonesian Android Community [id-android]" 

3. K9 ga bs send mail

4. Doubts while doing app's with graphics

5. urgent help Integrating code with Android home screen code.

6. IP Adress of android device

7. paging of a listview