0

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!

1
  • You created application class, but tring to make it as activity. Its wrong. What you exactly need, activity class or application class? Commented Feb 23, 2018 at 7:11

1 Answer 1

2

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>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the reply, but I already tried this (Android Studio suggested it), but it only causes my app to crash. No other error is displayed to the console
@Rufus Remove all the TODO function calls and it will stop crashing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.