So I've created a widget, and I'm tried my best to simulate a toggle button. Sense Widget doesn't support a toggle button I'm using a imagebutton and plan on changing the image depending on it's state.
Only problem is it's state isn't being saved and I'm not sure why.
package com.tdubstudios.beastmode;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
import android.widget.Toast;
public class WidgetClass extends AppWidgetProvider{
private static final String ACTION_WIDGET_RECEIVER = "ActionRecieverWidget";
private RemoteViews views;
public boolean beastmodeOn = false;
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
final int N = appWidgetIds.length;
views = new RemoteViews("com.tdubstudios.beastmode", R.layout.widget_layout);
// Perform this loop procedure for each App Widget that belongs to this
// provider
for (int i = 0; i < N; i++) {
int appWidgetId = appWidgetIds[i];
Intent intent = new Intent(context, WidgetClass.class);
intent.setAction(ACTION_WIDGET_RECEIVER);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.widget_ib, pendingIntent);
// Tell the AppWidgetManager to perform an update on the current App
// Widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) {
if(beastmodeOn){
beastmodeOn = false;
Toast.makeText(context, "beastmode is now off", Toast.LENGTH_SHORT).show();
}else{
beastmodeOn = true;
Toast.makeText(context, "beastmode is now ON", Toast.LENGTH_SHORT).show();
}
}
super.onReceive(context, intent);
}
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
super.onDeleted(context, appWidgetIds);
Toast.makeText(context, "onDelete has been called in widgetClass", Toast.LENGTH_LONG).show();
}
}
No matter how many times I press the button I always get the toast "beastmode is now ON"
Any suggestions?
Thank you.
EDIT:
Ok so I added this code:
Log.i("widget","BEFORE beastmode is: "+beastmodeOn);
beastmodeOn = true;
Toast.makeText(context, "beastmode is now ON", Toast.LENGTH_SHORT).show();
Log.i("widget","AFTER beastmode is: "+beastmodeOn);
and my log gives me this back:
BEFORE beastmode is: false
AFTER beastmode is: true
It gives me this EVERY time I press the button. So obviously it creates a new instance or something to that effect. Once it executes it must destroy all variable values or something, maybe someone with more knowledge knows the right words.
So does anyone know a work around then?