# Singleton in Android

**URL:** <https://discuss.kotlinlang.org/t/singleton-in-android/10653>\
**Category:** Android\
**Created:** [December 5, 2018, 10:54am UTC](https://discuss.kotlinlang.org/t/singleton-in-android/10653 "2018-12-05T10:54:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![gbattez](https://avatars.discourse-cdn.com/v4/letter/g/7ea924/32.png) [@gbattez](https://discuss.kotlinlang.org/u/gbattez)\
**Post date:** [December 5, 2018, 10:54am UTC](https://discuss.kotlinlang.org/t/singleton-in-android/10653/1 "2018-12-05T10:54:13Z")

</div>

Hi, so apparently using static variables in android can cause some problems, the most important one being the fact that the variable can lose it’s value when application is in background.

So what about variables in Kotlin’s objects or companion objects ?

---

<div class="post-metadata">

**Author:** ![SOF3](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/sof3/32/5646_2.png) [@SOF3](https://discuss.kotlinlang.org/u/SOF3)\
**Post date:** [December 5, 2018, 10:58am UTC](https://discuss.kotlinlang.org/t/singleton-in-android/10653/2 "2018-12-05T10:58:15Z")

</div>

You can try reading the bytecode. Kotlin objects are just implemented as Java static variables. In the end they are still only saved in the same Java runtime (not really Java on Android, but the principle is the same), not anywhere on the disk.

---

<div class="post-metadata">

**Author:** ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)\
**Post date:** [December 5, 2018, 6:10pm UTC](https://discuss.kotlinlang.org/t/singleton-in-android/10653/3 "2018-12-05T18:10:16Z")

</div>

You should read Android documentation, but as far as I remember, runtime can clear memory of any background application. Anything not parcelized could be erased.The same goes for kotlin object. Also you should remember that storing something but caches in global variables or singletons is a bad practice and should be avoided.

---

<div class="post-metadata">

**Author:** ![gbattez](https://avatars.discourse-cdn.com/v4/letter/g/7ea924/32.png) [@gbattez](https://discuss.kotlinlang.org/u/gbattez)\
**Post date:** [December 6, 2018, 1:02pm UTC](https://discuss.kotlinlang.org/t/singleton-in-android/10653/4 "2018-12-06T13:02:08Z")

</div>

Ok thanks for the clarifications, and what about storing enum functions in companion objects? like ‘enumFromChar(c: Char): AnEnum’  
or static functions in general ? I know kotlin’s extension functions are basically static functions

---

<div class="post-metadata">

**Author:** ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)\
**Post date:** [December 6, 2018, 1:14pm UTC](https://discuss.kotlinlang.org/t/singleton-in-android/10653/5 "2018-12-06T13:14:59Z")

</div>

You are mixing state and functions. Even if runtime erases the state, it does not erase the code, or more specifically, code is being loaded every time you load the application.

---

<div class="post-metadata">

**Author:** ![SOF3](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/sof3/32/5646_2.png) [@SOF3](https://discuss.kotlinlang.org/u/SOF3)\
**Post date:** [December 6, 2018, 3:06pm UTC](https://discuss.kotlinlang.org/t/singleton-in-android/10653/6 "2018-12-06T15:06:26Z")

</div>

To put it the other way, the functions get defind every time the application is loaded.

When we say that you shouldn’t store data in static/object properties, we are just saying that _changes on the property_ will not be stored over time.

Suppose you have this object:

```kotlin
object MyObject {
    val version = 123
    var checks = 0
}

```

If you run this code every time your MainActivity is created:

```kotlin
MyObject.checks++
Toast.makeText(this, "We are running version ${MyObject.version}. Checked ${MyObject.checked} times", Toast.LENGTH_LONG).show()

```

before anyone/any bots complains about no translation, this is just an example

You will constantly get “We are running version 123” even after your app is restarted. This is because we defined `version = 123` _when the `MyObject` class gets loaded_ (which gets unloaded every time the app gets restarted/destroyed or whatever). So in case the data in MyObject gets erased, it gets set again before you use it the next time.

On the other hand, “Checked x times” will increase by one every time the user goes back to MainActivity, but it resets every time. This is because `MyObject.checks` also gets erased and set again, but as you can see from the code, it always runs `MyObject.checks = 0`, so it always starts again with 0.
