Click here to Skip to main content
15,914,013 members
Please Sign up or sign in to vote.
3.33/5 (3 votes)
See more:
Hello

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:

C#
public class MainWidget extends AppWidgetProvider {

   private static int myNum = 0;

...


And in the onReceive event I do this (after checking the button was clicked):

MIDL
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:

C#
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.

Here is the complete code: click here to view[^]

-Faraz Azhar

@Nagy: No thats not the issue. I've put Toasts msgs on every event.. OnDeleted, OnDisabled, etc. So I know all these events are firing properly.

Have a look at this link below. Another person is also having the exact same problem. Maybe you can understand better in his wording:
Gmail Text Cloud Widget[^]
Posted
Updated 21-Jun-11 2:55am
v5
Comments
Nagy Vilmos 21-Jun-11 8:56am    
I've moved your Answer into the question. Unless you are answering your own question, edit the original question using the [Improve question] link or post a comment on the relevant thread.
Nagy Vilmos 21-Jun-11 8:57am    
Using a task mmonitor, is the app really shutting down? IF so it could be a persistance issue.
Faraz Azhar, Dubai 21-Jun-11 10:53am    
Yes the app is shutting down properly. I just read something on the net after hours of searching ... someone says that OnReceive event of the AppWidgetProvider is just a ghostly instance, it is initiated by a Broadcast. It is not linked with the rest of the class. So if the OnReceive is using any of variables from the class and tries to change it, the changes wont save in it. To save changes, one must use SharedPreferences or file or database to store values.

Is that true ? Kinda weird to believe. But is somewhat true seeing what's happening. If I set my variable to "5" at declaration (e.g. private int myNum = 5), and add 1 to it in OnReceive; the Toast displays 6. But it keeps displaying 6 everytime. Seems like the addition of 1 to variable is not being saved in memory. Very weird. Any clues on that ?

1 solution

When you remove the widget icon from the home screen it is not ending; this is normal behaviour. You are not actually closing the app, you are simple removing the icon. If you get a tool like Advanced Task Manager, you will eb able to see that it is still running.

It might be worth checking if the OnDeleted event is firing.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900