You will have to have some form of default value for this to work, but here
is how I do it:

When I want to read from SharedPreferences I do this:
SharedPreference prefMgr =
PreferenceManager.getDefaultSharedPreferences(this);
m_curPage = prefMgr.getInt(getString(R.string.wgt_pref_cur_screen_key), 0);

When I want to write to it I do this:
SharedPreference prefMgr =
PreferenceManager.getDefaultSharedPreferences(this);
Editor editor = m_prefMgr.edit();
editor.putInt(this.getString(R.string.wgt_pref_cur_screen_key), m_curPage);
editor.commit();

You don't actually pass an object.  SharedPreferences essentially abstracts
out reading to and writing from a file....

Thanks,
Justin

----------------------------------------------------------------------
There are only 10 types of people in the world...
Those who know binary and those who don't.
----------------------------------------------------------------------


On Thu, Nov 19, 2009 at 12:41 AM, Susan <ska...@gmail.com> wrote:

> Justin, thanks. But doesn't that still leave me with the same problem?
> IE, if WidgetConfiguration.java creates the SharedPreferences objet,
> how does it pass that object back to the code running in Widget.java?
>
> On Nov 19, 1:16 am, Justin Anderson <janderson....@gmail.com> wrote:
> > You could use SharedPreferences...  I have a Widget that displays
> multiple
> > "pages" of info to the user.  I use the SharedPreferences class to store
> > what page I am currently displaying, so that when the Widget is updated,
> it
> > knows which page to update.
> >
> > ----------------------------------------------------------------------
> > There are only 10 types of people in the world...
> > Those who know binary and those who don't.
> > ----------------------------------------------------------------------
> >
> > On Wed, Nov 18, 2009 at 11:32 PM, Susan <ska...@gmail.com> wrote:
> > > I have a widget whose appearance will depend on information gained
> > > from the user in the configuration activity. However, I'm uncertain
> > > how (and in what section of code) to act on the information gained
> > > from the user in the config.
> >
> > > Here is the target functionality from the user's perspective:
> >
> > >   When the user creates the widget, the config activity prompts for a
> > > string (with an EditText). The user types it in and presses the submit
> > > button. The config activity goes away and the widget appears on the
> > > homescreen displaying the text the user entered. When the user touches
> > > the widget, the browser is opened and goes to a particular URL that is
> > > determined in part by the string the user entered.
> >
> > > Here is what I have implemented so far:
> >
> > > The config activity lets the user enter data and happily displays it
> > > to me as a Toast message. Then the config goes away as desired and the
> > > widget appears with the default widget icon. If you touch the widget,
> > > it goes to a hardcoded URL. (code below)
> >
> > > My question:
> >
> > > How do I "connect" the info from the config activity with the code
> > > that displays the widget itself? If I put the user's string in a
> > > putExtra of the intent sent in setResult(), where do I write the code
> > > to retrieve that extra? It seems like it ought to put it somewhere in
> > > the AppWidgetProvider code, but where? (Or is there a better way to do
> > > it?)
> >
> > > Here are the relevant portions of my code:
> >
> > > ------------------------
> > >   widget.java:
> > > ------------------------
> > > public class Widget extends AppWidgetProvider
> > > {
> > >    public void onReceive(Context context, Intent intent)
> > >    {
> > >        String action = intent.getAction();
> > >        if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action))
> > >        {
> > >                //Toast.makeText(context, "onReceive()",
> > > Toast.LENGTH_SHORT).show();
> > >            RemoteViews views = new RemoteViews(context.getPackageName
> > > (), R.layout.widget);
> >
> > >            Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse
> > > ("http://www.cnn.com";));
> > >            i.addCategory(Intent.CATEGORY_BROWSABLE);
> > >            i.setComponent(new ComponentName("com.android.browser",
> > > "com.android.browser.BrowserActivity"));
> > >            PendingIntent pendingIntent = PendingIntent.getActivity
> > > (context, 0, i, 0);
> > >            views.setOnClickPendingIntent(R.id.Widget, pendingIntent);
> >
> > >            AppWidgetManager.getInstance(context).updateAppWidget
> > > (intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS),
> > > views);
> > >        }
> > >    }
> > > }
> > > // end of widget.java
> >
> > > ------------------------------------------------
> > >   WidgetConfiguration.java
> > > ------------------------------------------------
> > > public class WidgetConfiguration extends Activity {
> >
> > >    EditText myEditText;
> >
> > >    @Override
> > >        public void onCreate(Bundle icicle) {
> > >        super.onCreate(icicle);
> >
> > >        Intent intent = new Intent();
> > >        intent = getIntent();
> > >        Bundle extras = intent.getExtras();
> > >        int appWidgetId = extras.getInt
> > > (AppWidgetManager.EXTRA_APPWIDGET_ID);
> > >        setResult(RESULT_CANCELED);
> >
> > >        // Set the view layout resource to use.
> > >        setContentView(R.layout.config);
> >
> > >        // Find the EditText
> > >        myEditText = (EditText)findViewById(R.id.txt_url);
> >
> > >            View.OnClickListener mOnClickListener = new
> View.OnClickListener
> > > () {
> > >                public void onClick(View v) {
> >
> > >                        Intent intent = new Intent();
> > >                        intent = getIntent();
> > >                        setResult(RESULT_OK, intent);
> > >                        Toast.makeText(getBaseContext(),
> > > myEditText.getText(),
> > > Toast.LENGTH_SHORT).show();
> > >                        finish();
> > >                }
> > >            };
> >
> > >        // Bind the action for the submit button.
> > >        findViewById(R.id.btn_urlSubmit).setOnClickListener
> > > (mOnClickListener);
> >
> > >        // If they gave us an intent without the widget id, just bail.
> > >        if (appWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
> > >                finish();
> > >            }
> > >    }
> > > }
> > > //end of WidgetConfiguration.java
> >
> > > --
> > > You received this message because you are subscribed to the Google
> > > Groups "Android Beginners" group.
> > > To post to this group, send email to
> android-beginners@googlegroups.com
> > > To unsubscribe from this group, send email to
> > > android-beginners+unsubscr...@googlegroups.com<android-beginners%2bunsubscr...@googlegroups.com>
> <android-beginners%2bunsubscr...@googlegroups.com<android-beginners%252bunsubscr...@googlegroups.com>
> >
> > > For more options, visit this group at
> > >http://groups.google.com/group/android-beginners?hl=en
> >
> >
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Beginners" group.
> To post to this group, send email to android-beginners@googlegroups.com
> To unsubscribe from this group, send email to
> android-beginners+unsubscr...@googlegroups.com<android-beginners%2bunsubscr...@googlegroups.com>
> For more options, visit this group at
> http://groups.google.com/group/android-beginners?hl=en
>

-- 
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to android-beginners@googlegroups.com
To unsubscribe from this group, send email to
android-beginners+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en

Reply via email to