If you want an object to be associated with the class instead of having one for each instance you need to declare it as static
. That means that each time your create an instance of the containing class they will all reference the same object.
Android widget on every ImageView click instantiates new object
-
28-09-2022 - |
Question
I'm brand new to developing widgets for android so please bear with me if this is a stupid question. I'm writing my own mp3 player specific to my needs. I have it working great as an app, but now I'd like to pull it out into a homescreen widget as well (more for the learning exercise than anything else).
I split out my mp3 player logic into a separate class, and when I try and intantiate an object from that class with a button click from my widget, it works fine. The problem is, I can't seem to use that same object for the next time that button is clicked or any other button for that matter. Instead, it recreates a new object each time a button is clicked inside my widget. I'm just trying to make my play/pause ImageView function properly.
Here is the code for my WidgetProvider
package com.example.musicplayerforburrito;
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;
public class MyWidgetProvider extends AppWidgetProvider {
public static String WIDGET_PLAY_BUTTON = "android.appwidget.action.PLAY_PAUSE_WIDGETS";
MusicPlayerClass mpc = new MusicPlayerClass();
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
ComponentName watchWidget = new ComponentName(context, MyWidgetProvider.class);
remoteViews.setOnClickPendingIntent(R.id.playpausewidget, getPendingSelfIntent(context, WIDGET_PLAY_BUTTON));
appWidgetManager.updateAppWidget(watchWidget, remoteViews);
}
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
super.onReceive(context, intent);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews remoteViews = new RemoteViews( context.getPackageName(), R.layout.widget_layout );
ComponentName watchWidget = new ComponentName(context, MyWidgetProvider.class);
if (WIDGET_PLAY_BUTTON.equals(intent.getAction())) {
if(!mpc.hasFolders)
mpc.Initialize();
if(!mpc.isPlaying)
{
remoteViews.setTextViewText(R.id.mp3filename, mpc.StartSong());
remoteViews.setImageViewResource(R.id.playpausewidget, R.drawable.pause);
}
else
{
mpc.PauseMusic();
remoteViews.setImageViewResource(R.id.playpausewidget, R.drawable.play);
}
appWidgetManager.updateAppWidget(watchWidget, remoteViews);
}
}
protected PendingIntent getPendingSelfIntent(Context context, String action) {
Intent intent = new Intent(context, getClass());
intent.setAction(action);
return PendingIntent.getBroadcast(context, 0, intent, 0);
}
}
any suggestions would be greatly appreciated.
TIA
Solution