Yousha Aleayoub Yousha Aleayoub - 3 months ago 20
Android Question

Android: Locations of super() calls in Widgets

When creating own Widget class, we are overriding some of the basic

AppWidgetProvider
methods:

@Override
public void onEnabled(final Context context)
{
// TODO Auto-generated method stub
super.onEnabled(context);
}
@Override
public void onDeleted(final Context context, final int[] appWidgetIds)
{
// TODO Auto-generated method stub
super.onDeleted(context, appWidgetIds);
}
@Override
public void onDisabled(final Context context)
{
// TODO Auto-generated method stub
super.onDisabled(context);
}
@Override
public void onUpdate(final Context context, final AppWidgetManager appWidgetManager, final int[] appWidgetIds)
{
// TODO Auto-generated method stub
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
@Override
public void onReceive(final Context context, final Intent intent)
{
// TODO Auto-generated method stub
super.onReceive(context, intent);
}


Does it perform any pre-processing when we call super() at the End or Start?

Is there a rule/explanation when to call super()?

Answer

The AppWidgetProvider class is just a BroadcastReceiver with a specialized onReceive() method that delegates incoming broadcasts to the other methods based on the action. The onEnabled(), onDeleted(), onDisabled(), and onUpdate() methods are all empty in the AppWidgetProvider class, so there's no need to call the super method in any of those.

You don't need to override onReceive() at all, and can just handle your widget's functionalities in the other provided methods. If you intend to override the onReceive() method and handle everything yourself - that is, check the action, and act accordingly in each case - then you can omit the super call there as well. If you're going to "go halfway", and just handle some actions directly, then you just need to call super.onReceive() when appropriate, as all it's really going to do is immediately call one of the other methods.

Comments