[FIXED] Helper class best approach android

Issue

I was looking at Google I/O Android App iosched link and saw that they use mostly static methods in their helper/util classes. However, I have found many people not recommending the use of static methods in helper classes.

Let’s say if I have 3 activities that are doing some work like showing alert dialog or notification, then I need to add the same code in all the 3 activities. What if I am writing in files from 10 different activities. Isn’t using a helper class with a static method a better approach than writing same code again and again? if not then what is the best approach.

public class NotificationHelper {

  /**
   * create notification
   * @param context activity context
   * @param title notification title
   * @param contentText notification text
   * @param mNotificationId notification id
   */
  public static void setUpNotification(Context context, String title, String contentText, int mNotificationId) {

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(context).setLargeIcon((BitmapFactory.decodeResource(context.getResources(),R.drawable.launcher)))
                    .setSmallIcon(R.drawable.ic_notif)
                    .setContentTitle(title)
                    .setContentText(contentText).setPriority(NotificationCompat.PRIORITY_MAX);

    Intent resultIntent = new Intent(context, MainActivity.class);
    PendingIntent resultPendingIntent =
            PendingIntent.getActivity(
                    context,
                    0,
                    resultIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(resultPendingIntent);
    mBuilder.setOngoing(true);
    NotificationManager mNotificationManager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(mNotificationId, mBuilder.build());
}

  /**
   * cancel notification
   * @param ctx context
   * @param notifyId id of notification to be cancelled
   */
  public static void cancelNotification(Context ctx, int notifyId) {
    NotificationManager nMgr = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
    nMgr.cancel(notifyId);
  }
}

Solution

Usuage of Helper classes is little debatable in object oriented programming. You can use normal class and include the object of the class. Or you may put common code in a base class and then extend it. But if we decide to use Helper classes then below are the few points which may help you as guideline.

  1. Helper classes are the utility entities. They are better used just like utility so prevent the instantiation and extension by marking the default constructor as private.

  2. Expose the ‘ static ‘ methods. See if the methods are just needed by the classes in the package as of the utility class, then keep the acess modifer as package-private, and if needed by classes outside also then you can make them public. The intent is to prevent exposing package details too much by public APIs. You can also try to have abstractions in parametrs and return type.

  3. Try to keep such classes as stateless by not having fields. Keeping the ( static ) fields may lead referencing objects even when not needed.

  4. Name such classes properly to let users of the helper class know its intention and also that they are just utility classes. Also name the methods according to use and to minimize the confusions.

Answered By – nits.kk

Answer Checked By – Cary Denson (Easybugfix Admin)

Leave a Reply

(*) Required, Your email will not be published