4

The Code A is extension function for Context, Fragment and Activity.

I think it's redundant, how can I optimize it?

Code A

    fun Context.toast(msg: String){
        Toast.makeText(this, msg, Toast.LENGTH_LONG).show()
    }
    
    fun Context.toast(@StringRes resId: Int){
        toast(getString(resId))
    }
    
    fun Context.toast(@StringRes resId: Int,msg: String){
        toast(getString(resId) + msg)
    }
    
    fun Context.toast(msg: String,@StringRes resId: Int){
        toast(msg + getString(resId))
    }
    
    //------------------------------------------------

    fun Fragment.toast(msg:String) {
        requireContext().toast(msg)
    }
    
    fun Fragment.toast(@StringRes resId: Int) {
        toast(requireContext().getString(resId))
    }
    
    fun Fragment.toast(@StringRes resId: Int, msg: String) {
        toast(requireContext().getString(resId) + msg)
    }
    
    fun Fragment.toast( msg: String, @StringRes resId: Int) {
        toast(msg+ requireContext().getString(resId))
    }
    
    //------------------------------------------------

    fun Activity.toast(msg: String){
        Toast.makeText(this, msg, Toast.LENGTH_LONG).show()
    }
    
    fun Activity.toast(@StringRes resId: Int){
        toast(getString(resId))
    }
    
    fun Activity.toast(@StringRes resId: Int,msg: String){
        toast(getString(resId) + msg)
    }
    
    fun Activity.toast(msg: String,@StringRes resId: Int){
        toast(msg + getString(resId))
    }
Jenea Vranceanu
  • 4,530
  • 2
  • 18
  • 34
HelloCW
  • 843
  • 22
  • 125
  • 310
  • 2
    you don't need to implement extension functions for `Activity`, since `Activity` extends `Context` – IR42 Aug 23 '20 at 07:28

1 Answers1

6

Context extension functions should be enough. You will be able to use it virtually in any place where a UI component is available.

We can remove extensions for Activity because Activity is an indirect subclass of Context class.

And we also can remove Fragment extensions because they are not useful until a fragment is attached to an activity (which is the context).

fun Context.toast(msg: String){
    Toast.makeText(this, msg, Toast.LENGTH_LONG).show()
}

fun Context.toast(@StringRes resId: Int){
    toast(getString(resId))
}

fun Context.toast(@StringRes resId: Int,msg: String){
    toast(getString(resId) + msg)
}

fun Context.toast(msg: String,@StringRes resId: Int){
    toast(msg + getString(resId))
}

These functions can be used in an activity:

Use of extension functions in Activity

And in a fragment. It is logically correct and intentional that we must first get a reference to a Context:

Use of extension functions in Fragment

Jenea Vranceanu
  • 4,530
  • 2
  • 18
  • 34
  • requireContext() can throw illegalStateException if Fragment is not attached! – Arpit J. Jul 04 '22 at 13:50
  • @ArpitJ. Correct. That is stated in the documentation of this method: https://developer.android.com/reference/androidx/fragment/app/Fragment#requireContext(). – Jenea Vranceanu Jul 05 '22 at 16:49