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?
- Create Kotlin file, e.g. named
Utils
; 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 withoutContext
parameter:fun Activity.checkConnectivity(): Boolean {
val cm = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val activeNetwork =cm.activeNetworkInfo
return activeNetwork != null && activeNetwork.isConnectedOrConnecting
}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
How to Add a Newline to a Textview in Android
How to Kill Native Applications from 'Adb Shell'
How to Get the Full File Path from Uri
Error:Binderproxy@45D459C0 Is Not Valid; Is Your Activity Running
How to Implement My Custom Toolbar in My Fragment
Alertdialog Dialog.Dismiss(), Dialog.Close() Not Working
Admob Ads Not Loading - Failed to Load Ad: 0
Android: How to Enable/Disable Option Menu Item on Button Click
How to Convert Utc Timestamp to Device Local Time in Android
Force Video to Open in Youtube App on Android
Android Prevent Back Button from Closing the App
Disable Ssl as a Protocol in Httpsurlconnection
How to Disable Edittext in Android
How to Set the Image from Drawable Dynamically in Android
How Remove Unnecessary Space Between Cards in Recyclerview
How to Access Data/Data Folder in Android Device