1

I initialize one variable in an onUpdate() method and after that I call onReceive() function which runs fine but cannot access varible set in onUpdate() method. Why is that? Those varible is string variableand are declared public. Am I missing something?

public class WidgetTest extends AppWidgetProvider {
public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget";
public String state;
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) 
{
    Log.e("UPDATE", "Start");   
    RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
     state="State_update"

 System.out.println(state);// My variable is initilised
    Intent active = new Intent(context, WidgetTest.class);
    active.setAction(ACTION_WIDGET_RECEIVER);       
    PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
    remoteViews.setOnClickPendingIntent(R.id.buttonclick, actionPendingIntent);

    super.onUpdate(context, appWidgetManager, appWidgetIds);
    appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
    Log.e("UPDATE", "End");
}

@Override
public void onReceive(Context context, Intent intent) 
{
   super.onReceive(context, intent);
    Log.e("RECEIVE", "Start 2");
    if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) 
    {

            Log.e("Test", "My State is  "state);//it gives me null point exception;

    }
   Log.e("RECEIVE", "End");


}

state varible in onReceive gives null point exception

Shakeeb Ayaz
  • 6,200
  • 6
  • 45
  • 64

1 Answers1

2

for a AppWidgetReceiver , first onReceive() will be called and then based on the Action received, it will call onUpdate(...) method. so here you are initializing state in onUpdate() which will be called after onReceive(), thus state is null in onReceive().

Community
  • 1
  • 1
Gopal Gopi
  • 11,101
  • 1
  • 30
  • 43
  • but if I delete and get back the widget again then onUpdate is called first not the onreceive and my variable are be initilized then also I am getting null point in onReceive – Shakeeb Ayaz Dec 18 '13 at 06:50
  • I am getting varible value in println of onUpdate – Shakeeb Ayaz Dec 18 '13 at 06:53
  • 1
    every time system will create a new instance of BroadcastReceiver and calls onReceive() method. and here your state variable is instance variable and will be null every time object is created. to overcome this problem, you can declare state variable as static – Gopal Gopi Dec 18 '13 at 06:56
  • this is what i wanted – Shakeeb Ayaz Dec 18 '13 at 06:58
  • @ShakeebAyaz some more ans??? do you need further explanation? if so, what can I help you? – Gopal Gopi Dec 18 '13 at 07:08
  • then how can get that state value in Onreceive – Shakeeb Ayaz Dec 18 '13 at 07:22
  • why don't you intialize it in onReceive()? (make state as static) – Gopal Gopi Dec 18 '13 at 07:25
  • actully my widget frezez when memeory is cleaned from task manager so I though my varible in onupdate are null when memory is cleaned..but it is always null whether i memory is cleaned or not..Here is my orignal question http://stackoverflow.com/questions/20630888/widget-does-not-works-after-clear-memory can u plz help me out – Shakeeb Ayaz Dec 18 '13 at 07:30
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/43400/discussion-between-gopal-rao-and-shakeeb-ayaz) – Gopal Gopi Dec 18 '13 at 07:36