Rufus
Rufus

Reputation: 33

How to implement Kotlin class from java in android?

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!

Upvotes: 0

Views: 1615

Answers (1)

Saritha G
Saritha G

Reputation: 2608

Your inner class i.e., AppLifecycleTracker class need to override few more methods.

  import android.app.Activity
  android.app.Application
  import android.os.Bundle


/**
 * Created by saritha high 13/2/18.
 */
class AirventApplication : 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");
          }
      }

      override fun onActivityPaused(p0: Activity?) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
       }

      override fun onActivityResumed(p0: Activity?) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
      }

      override fun onActivityDestroyed(p0: Activity?) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
      }

      override fun onActivitySaveInstanceState(p0: Activity?, p1: Bundle?)  {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
      }

      override fun onActivityCreated(p0: Activity?, p1: Bundle?) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
      }
 }
}

And declare your application class in Manifest like:

  <application
    android:name=".YourApplication"
    android:icon="@mipmap/app_icon"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">
    <activity
        android:name=".YourActivity"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
   </application>

Upvotes: 2

Related Questions