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. Upgrade to m5-rc15

I'm still using m5-rc14, shall I upgrade to rc15?
On some doc pages there are even references to m5-rc15b
but I couldn't find any specifics.
What's the difference?
I'd appreciate correction of existing bugs but I'm afraid
of new ones :-) at this late stage, just two weeks before
the Challenge deadline.

Is the Eclipse plug-in affected too?

Thanks,
Kosmaj

--~--~---------~--~----~------------~-------~--~----~

2. Source code for android.R.layout.simple_list_item_1

I would like to override some of the default behavior given by
android.R.layout.simple_list_item_1.  Where would I find the xml
layout for simple_list_item_1?

Thanks
--~--~---------~--~----~------------~-------~--~----~

3. Query People.CONTENT_URI by ID, return sql exception (very simple code with exception)

4. Get a set of contacts

5. Problems with animation on two views running parallel.

6. Extension of Android Contact Schema

7. traceview fails on Mac OS X 10.5.2 ?