I'm a Java/Kotlin newbie, working on an android app. I tried to implement the following into YourApplication.kt, that I found here: https://stackoverflow.com/a/42679191/4666306
package com.tijaname.fortysix
import android.app.Activity
import android.app.Application
import android.os.Bundle
import android.util.Log.println
class YourApplication : Application() {
override fun onCreate() {
super.onCreate()
registerActivityLifecycleCallbacks(AppLifecycleTracker())
}
}
class AppLifecycleTracker : Application.ActivityLifecycleCallbacks {
private var numStarted = 0
override fun onActivityStarted(activity: Activity?) {
if (numStarted == 0) {
println("Activity has started");
}
numStarted++
}
override fun onActivityStopped(activity: Activity?) {
numStarted--
if (numStarted == 0) {
println("Activity has stopped");
}
}
}
I added
ext.kotlin_version = '1.1.51'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
to my gradle scripts. I also added this to my manifest:
<activity android:name=".YourApplication"></activity>
I tried to invoke the kotlin script like this (from my MainActivity in java):
Intent intent = new Intent(getBaseContext(), YourApplication.class);
startActivity(intent);
But I get the following error:
Class 'YourApplication' is not abstract and does not implement abstract member public abstract fun onActivityResumed(p0: Activity!)...
I followed Android-Studio's suggestions to make it abstract, but that led to more errors.
I also tried to 'Implement members' per AS's suggestion, but this makes my app crash in the emulator.
Thank you!