1

We know that Application class behaves as a singleton so we can store there some app session data, and interact to it from different screens. For example we do this in Java like so:

public class App extends Application {

    public static String myString;

    @Override
    public void onCreate() {
        super.onCreate();
    }

    public static String getMyString() {
        return myString;
    }

    public static void setMyString(String myString) {
        App.myString = myString;
    }

}

And we ca use it like so App.setMyString("blah blah") and access this value from everywhere like so: App.getMyString()

I tried to change this to kotlin with no success:

class App : Application() {

    override fun onCreate() {
        super.onCreate()
    }

    companion object {
        var myString: String = null

        fun setMyString(str: String) {
            myString = str
        }

        fun getMyString(): String? {
            return myString
        }
    }

    init {
        myString = String()
    }
}

That variable doesn't keep my value

Choletski
  • 7,074
  • 6
  • 43
  • 64
  • can you share how/where you're setting this value? – r2rek Nov 04 '19 at 11:57
  • The code you are showing does not have any issues. It should work perfectly but there could be an issue with the way you are accessing your 'App' object – Pavan Varma Nov 04 '19 at 12:01
  • 1
    Why do you need Application class to access static variables? You can declare and use static variables from any class. If you are trying to make a singleton, you should avoid making the variables static, or in case of kotlin, take it out of companion object. – Harikrishnan Nov 04 '19 at 12:03
  • Are you declaring the use of that class as the Application class in the app manifest? On a side note, you might want to consider carefully you if you want to go that approach. SharedPreferences can do the same thing if you want to persist primitive or String values application wide. – Ryujin Nov 04 '19 at 12:05
  • See this answer https://stackoverflow.com/questions/37391221/kotlin-singleton-application-class/37412029#37412029 – Vitalii Malyi Nov 04 '19 at 12:15
  • There is no reason to extend Application for your singleton in the first place. – Tenfour04 Nov 04 '19 at 15:23

3 Answers3

1

If you want to use Singleton pattern and if you are not dependent to anything from Application, Activity or Fragment, then the best solution is to use object.

Like this

object MyString {

    var myString: String = String()

    fun setMyStringData(str: String) {
        myString = str
    }

    fun getMyStringData(): String = myString

}

Use of this is

MyString.setMyStringData("string")
MyString.getMyStringData()

This won't pollute your other classes and you will achieve what you wished for. If you need anything from any class then you should use companion object in that class.

Milan Kundacina
  • 309
  • 1
  • 8
0

Here is the way you can assign the value to your variable

class App : Application() {

    override fun onCreate() {
        super.onCreate()
       
    }

    companion object {
        var myString: String? = null

        fun setMyStringData(str: String) {
            myString = str
        }

        fun getMyStringData(): String? {
            return myString
        }
    }

    init {
        myString = String()
    }


}

How to use For set data

App.myString =" I assigned the data"

App.setMyStringData("I am assigning the data")

For getting the data

Log.i(TAG, App.myString)

Log.i(TAG, App.getMyStringData ?: "I am null")

You are facing any issue with setMyString and getMyString which is already defined in java so just change the name of the function it works for you as well.

Community
  • 1
  • 1
Sunny
  • 3,134
  • 1
  • 17
  • 31
0

Helpful:

class App : Application() {

    override fun onCreate() {
        super.onCreate()

        myString = String()
   
    }

    companion object {
        var myString: String? = null

        fun setMyStringData(str: String) {
            myString = str
       }

        fun getMyStringData(): String? {
            return myString
        }
    }

}

Now u can use it like below in anywhere:

App.setMyStringData("Test")
Hadi Note
  • 1,386
  • 17
  • 16