*> "Please use a file or a database for your data."* As usual, Mark gave you the answer you needed...
The receiver is probably getting destroyed between each onReceive call. Then when it is recreated the value is 0 again. What I do for receivers is to store simple persistent info in a SharedPreferences object. The other option, for more complicated stuff, is to use a database. Thanks, Justin Anderson MagouyaWare Developer http://sites.google.com/site/magouyaware On Tue, Jun 21, 2011 at 1:26 AM, Faraz Azhar <[email protected]> wrote: > I would always avoid using static variables. In fact I'm not even > trying to achieve what static provides. > > My problem is that if i declare my variable in the most simplest > manner, that's not working at all. e.g: > private int myNum = 0; > > If I increase the value ( myNum += 1; ) then its not storing that > value in the number. It resets to zero every time I check the value of > variable. > > Please see my code: > http://pastebin.com/EGRE41gh > > Every time I increase the value of myNum by 1, it appears that the > value was previously zero and has now increased to become "1". It > never becomes 2, and 3 and further. > > Could it be because of threading or pendingIntent call ? > > On Jun 21, 5:21 am, Mark Murphy <[email protected]> wrote: > > 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/commonsguyhttp://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 > -- 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

