Android Kotlin Call a Function in Other Activity

Android Kotlin call a function in other activity

I would rather extract all logic to a different helper or utility class. It's a big mistake to have it within an activity if you're gonna reuse it. A pretty neat solution could be to have a ConnectivityUtils utility class like the famous iosched project has, just passing the application context to it:

/**
* Utility methods for dealing with connectivity
*/
object ConnectivityUtils {
fun isConnected(context: Context): Boolean {
val connectivityManager = context.applicationContext.getSystemService(
Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val activeNetworkInfo = connectivityManager.activeNetworkInfo
return activeNetworkInfo != null && activeNetworkInfo.isConnectedOrConnecting
}
}

Then, you just need to invoke it within any activity like this:

ConnectivityUtils.isConnected(this)

How to call a method of another activity in Kotlin?

for a quick solution, You can declare a static variables for data that You received in onNotificationReceived of ControlActivity

To crate a static variables, You will define those variables inside companion object

And you can get that data via class name like ControlActivity.foo inside your OnlineActivity (where foo is the static variable inside ControlActivity)

Kotlin: Calling a function from another class

Try to change your implementation like below:

Sound class:

class SoundEngine {

private var soundPool: SoundPool

init {
soundPool = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
val audioAttributes = AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.setUsage(AudioAttributes.USAGE_GAME)
.build()
SoundPool.Builder()
.setMaxStreams(1)
.setAudioAttributes(audioAttributes)
.build()
} else {
SoundPool(1, AudioManager.STREAM_MUSIC, 0)
}
}


fun load(context: Context, rawId: Int, priority: Int):Int {
return soundPool.load(context, rawId, priority)
}

fun play(soundID: Int, leftVolume: Float, rightVolume: Float, priority: Int, loop: Int, rate: Float) {
soundPool.play(soundID, leftVolume, rightVolume, priority, loop, rate)
}
}

MainActivity:

class MainActivity : AppCompatActivity() {
private var soundPool: SoundPool? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val soundEngine = SoundEngine()
val sound1 = soundEngine.load(this, R.raw.ahem_x, 1)

button1.setOnClickListener { soundEngine.play(sound1, 1f, 1f, 1, 0, 1f) }

}

}

How to call this function from other activities?

  1. Create Kotlin file, e.g. named Utils;
  2. Move function to that file, and add Context parameter:

    fun checkConnectivity(ctx: Context): Boolean {
    val cm = ctx.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
    val activeNetwork =cm.activeNetworkInfo
    return activeNetwork != null && activeNetwork.isConnectedOrConnecting
    }

    If you intend to use it only in Activity you can create extension function without Context parameter:

    fun Activity.checkConnectivity(): Boolean {
    val cm = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
    val activeNetwork =cm.activeNetworkInfo
    return activeNetwork != null && activeNetwork.isConnectedOrConnecting
    }
  3. Call that function from wherever you want. If you call it from Activity just use code:

    checkConnectivity(this@YourActivity)

    If you created extension function, just call it in Activity without passing any parameters:

    checkConnectivity()


Related Topics



Leave a reply



Submit