6
\$\begingroup\$

I made this method for a better Toast function. But I have doubts about it because this Toast forced me to make two methods while using two types of contexts.

Now I would like to improve it by shortening my code and choosing a better and more professional approach (overall structure, splitting onto logical units, accordance to code conventions, etc.)

public static final void iToast(final Activity context, final int resourceId)
{
   Utils.iToast(context, context.getResources().getString(resourceId));
}

public static final void iToast(final Activity context, final String message)
{
   if (context == null)
   {
      return;
   }

   context.runOnUiThread(new Runnable()
   {
      @Override
      public void run()
      {
         Toast.makeText(context.getApplicationContext(), message, Toast.LENGTH_LONG).show();
      }
   });
}
\$\endgroup\$
3
  • 2
    \$\begingroup\$ Welcome to CR! This question has great potential - if you included a bit more context about exactly what's toasting and why (how about including the whole class?), you have a stellar post waiting to happen. We like punny titles - but what we like even more is titles that tell us what the code does, not what you would like to get out of a peer review. Cheers! \$\endgroup\$ – Mathieu Guindon Sep 21 '15 at 14:57
  • 1
    \$\begingroup\$ Okay following the logical reasons you would want to do it the way you have: You need final in order to pass the parameters into runOnUiThread(Runnable). You need runOnUiThread(Runnable) because you want to call toast from a long running thread in the background. You need to call toast because you figure it's a good idea to send messages back to the user about what is happening in the background. \$\endgroup\$ – Victor Sep 26 '15 at 10:09
  • 1
    \$\begingroup\$ Now if I got all that right, I think a better way to do this is by using a Handler with messages. developer.android.com/reference/android/os/Handler.html then on the main thread where you define onHandleMessage you can generate the toast. and your toasts will not need to use final. and it's much easier to pass in the context. \$\endgroup\$ – Victor Sep 26 '15 at 10:10
4
\$\begingroup\$

I suggest you to use the new SnackBar class. It's always important to update the code with the latest features of android, that can improve your code and solve some android issues.

Your helper method might be something like this:

public static void showToast(final View view, final String toastMessage, final String callbackMessage, final Callable<Void> callback) {

    Snackbar snackBar = Snackbar
            .make(view, toastMessage, Snackbar.LENGTH_LONG);

    if (callback != null) {
        snackBar.setAction(callbackMessage, new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                try {
                    callback.call();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    snackBar.show();
}

The Callable parameter allow you to implement a Callable Interface so you can use the Command Pattern for calling a callback method.

Callable myCallback = new Callable<Void>() {

    @Override
        public Void call() {
            // Here you can do something as try to reload data from  
            // content provider or just nothing.
            return null;
        }
    });
}

The helper's method can then be called from every fragment, activity etc:

Utilities.showToast(getView(), "NO ITEMS", "RELOAD", myCallback);
\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.