Skip to content Skip to sidebar Skip to footer

Create Toast By A Static Method

I use this code in all my app fragments and it should be better if I use a static method. How can I do it? This static method should also works on fragment, not only activities. M

Solution 1:

Use this:

publicstaticvoidshowToast(Context context, String text) {
    Toast.makeText(context, text, Toast.LENGTH_LONG).show();
}

now for calling this method you should call like this:

ClassName.showToast(context,"text");

Here classname is class that containing static method.

Solution 2:

  • Change the signature of the method and add given Context as parameter
  • Change the signature of the method to make it static
  • Pass the Context as first argument of your Toast.makeText call

Solution 3:

publicstaticvoidchangeActivity(Context context,Class that){
    context.startActivity(newIntent(context, that));
} // my static methodpublicvoidforgotPass(View view){
    Function.changeActivity(LoginActivity.this,ForgotPass.class);
} // my activity i want to change

This is my solution, hope it can help

Post a Comment for "Create Toast By A Static Method"