views:

53

answers:

2
+1  Q: 

Save Widget State

the document http://developer.android.com/guide/topics/data/data-storage.html

shows that there are multiple ways to save data, I need to do this in a widget and everytime I try to save i get errors...

for instance

      SharedPreferences settings = getSharedPreferences("NAME", 0);
      SharedPreferences.Editor editor = settings.edit();
      editor.putBoolean("silentMode", false);

      // Commit the edits!
      editor.commit();

Error

Description Resource    Path    Location    Type
The method getSharedPreferences(String, int) is undefined for the type AWidget

another attempt:

     String FILENAME = "hello_file";
     String string = "hello world!";

     FileOutputStream fos = openFileOutput("Test.txt", Context.MODE_PRIVATE);
     fos.write(string.getBytes());
     fos.close();

with error

Description Resource    Path    Location    Type
The method openFileOutput(String, int) is undefined for the type AWidget

whats the deal? I see no mention this does not work in a widget so why is it that these examples don't work for me?

What is the preferred way to save this data?

+1  A: 

Hi Morty, I think if you want to save state about a particular widget it's best to use the saved state mechanic built into the View framework. SharedPreferences are sort of global to the Application. So if you had two instances of AWidget it would be rather hard to differenciate between the two at runtime.

Instead you might want to override:

onRestoreInstanceState(Parcelable state)

onSaveInstanceState()

If save is enabled for your View, Android should call onSaveInstanceState() where your widget will have a chance to return a Parcelable that it can use later in onRestoreInstanceState() to resume where it took off.

Greg
all i need to do is associate a widget ID with an int(which is passed into the function) I don't need to save the state of a view... just want to save which picture they set as a background when the phone restarts
morty346
I guess maybe I don't know what your saying, but this is for a widget, a widget does not have onSaveInstanceState
morty346
This is why others are asking for the base class because we cannot understand what that means. I was assuming you were using the word widget as in UI modules or Views. I assume now you mean Application Widget. please post more code or give more context if you want a more accurate answer
Greg
A: 

So the issue is is that I cannot just use this function without substance the above code will work fine if I do it with context. in front of it...

 SharedPreferences settings = context.getSharedPreferences("NAME", 0);
      SharedPreferences.Editor editor = settings.edit();
      editor.putBoolean("silentMode", false);

      // Commit the edits!
      editor.commit();

So as you can see that is all that was need to get the shared preferences... and with this...

int[] allids = AppWidgetManager .getInstance(context) .getAppWidgetIds(new ComponentName(context, AwarenessWidget.class));

I can get all the IDs of my app and call onupdate and update each ones views based upon the saved preferences

I can elaborate more if anyone needs...

Baffels me no one was able to figure that one out and help me! seems very straight forward now!

morty346