0

In Java and Android, we can do this:

public static MyApplication extends Application {
   private static Context appContext;

   public void onCreate() {
      appContext = this;
   }

   public static Context getAppContext() {
      return appContext;
   }
}

so that, somewhere else, we can do this:

appContext = MyApplication.getAppContext();

How do we do this in Kotlin? I've been going round in circles for the past hour or so.

Thanks in advance.

//Edit Perhaps I should have been clearer. I meant how can we write the above in Kotlin and use it in Kotlin.

the_new_mr
  • 3,476
  • 5
  • 42
  • 57
  • Just call appContext. I think Kotlin works with outside classes to make the calls like kotlin (but where possible use a getter instead in the Java byte code). Calling MyApplication.appContext should be enough – Zoe Aug 31 '17 at 19:00
  • 1
    Duplicate of https://stackoverflow.com/questions/40352684/what-is-the-equivalent-of-java-static-methods-in-kotlin ? – Xvolks Aug 31 '17 at 19:01
  • @Xvolks the question is about accessing a static field in Java code that happens to be private. This isn't a duplicate. – Zoe Aug 31 '17 at 19:03
  • 2
    @the_new_mr you shouldn't place context in static fields. It's a memory leak – Zoe Aug 31 '17 at 19:03
  • @LunarWatcher the OP wants to call `getAppContext()` statically like in the thread I've pointed out. Did I missed something? – Xvolks Aug 31 '17 at 19:05
  • Kotlin doesn't have getters and setters and usually refactors java code to kotlin (the byte code ends up differently, but that is how kotlin works and now related to the issue) – Zoe Aug 31 '17 at 19:07
  • I'm looking to write this code as Kotlin code. Problem is defining an equivalent static field that holds the app context that is accessible "statically" anywhere else. @LunarWatcher: i agree it's a memory leak for activities but not for application class since application always exists (over arching class) – the_new_mr Aug 31 '17 at 23:36
  • you can use companion object – Vijay Makwana Sep 01 '17 at 05:50

5 Answers5

5

In Kotlin this is called the 'companion object':

class MyApplication: Application {
    companion object {
        var appContext: Context? = null
            private set
    }
}
Brett Beatty
  • 5,690
  • 1
  • 23
  • 37
2

The key element I was missing was the use of an init block to set the appContext that is inside the companion object (I had already tried the companion object path but was struggling to actually get appContext set).

See code below:

class MyApplication : Application() {

    init {
        appContext = this
    }

    companion object {
        lateinit var appContext: Context
            private set
    }
}

This is then callable as usual via:

val testContext = MyApplication.appContext
the_new_mr
  • 3,476
  • 5
  • 42
  • 57
1

Assumed you have some java code in android and you want to convert it to kotlin code:

  1. Visit Link
  2. find Convert from java

it's help me to convert java code I've found on internet and converting it to kotlin code,

may this answer not help you about your question, but it would help you to convert what you know in java that you don't know how-to-do in kotlin

Flix
  • 444
  • 8
  • 20
0

you can use it this way

companion object{  
 //your static fields 
}

to call it from kotlin ==> ClassName.FieldName

to call it from java ==> ClassName.Companion.getFieldName()

Yassine BELDI
  • 562
  • 5
  • 16
  • You didn't answer the question: How do you call a static field/getter from kotlin? It isn't in a companion object, it originates in Java code – Zoe Aug 31 '17 at 19:30
  • I called `appContext = MyApplication.getAppContext();` a static java getter from Kotlin and it worked – Yassine BELDI Aug 31 '17 at 19:39
0

It seems like you want only one object of this class at runtime. This is called a singleton. There are recommendations to implement that properly in Java. Luckily Kotlin directly allows you to declare singleton objects on the top scope:

val o = object { your attributes and methods here}

fjf2002
  • 872
  • 5
  • 15