On Mon, Jun 20, 2011 at 4:01 AM, Faraz Azhar <[email protected]> wrote:
> I've created a simple home screen widget for the Android 2.2. It
> comprises of a button and a background image only. What im trying to
> do is that when user clicks on the button, i show a Toast msg with a
> number. This number is to be increased every time button is clicked,
> starting from zero.
>
> So ive declared my variable as follows:
>
> public class MainWidget extends AppWidgetProvider {
>
>    private static int myNum = 0;
>
> ...
>
>
> And in the onReceive event I do this (after checking the button was
> clicked):
>
> myNum += 1;
> Toast.makeText(context, "myNum:" + myNum, Toast.LENGTH_LONG).show();
>
> This works fine. Whenever user clicks the button the number is
> increased by 1 and displayed on screen. I want this number to be
> instance-specific. If i remove the widget from the home screen and add
> it back again, the numbering continues from the point where it was
> last time. For example if number is currently 4. I remove the widget
> and add back and click button, it will display 5. I want it to restart
> from 1.
>
> I've tried various declarations:
> private int myNum = 0;
> int myNum = 0;
> static int myNum = 0;
>
> But nothing work. If I remove the "static" keyword then the number
> doesn't increase at all. Every time the button is clicked, it keeps
> displaying 1. I want the number to be instance-specific i.e. to be
> restarted from 1 whenever the widget is removed and added back. Also
> if the user places more than one widgets on his home screen then both
> the widgets' numbering should be independent from each other. How do I
> achieve this? Seems like a simple task but totally confused how to
> declare my integer. Please anyone help.

You cannot reliably use static data members from manifest-registered
BroadcastReceivers like an AppWidgetProvider. Please use a file or a
database for your data.

-- 
Mark Murphy (a Commons Guy)
http://commonsware.com | http://github.com/commonsguy
http://commonsware.com/blog | http://twitter.com/commonsguy

Android Training in London: http://bit.ly/smand1, http://bit.ly/smand2

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to