12

We are trying to set the value of a global variable declared in the code below

 class MyApplication: Application() {
    var globalVar = 2
}

Now we have a Main Activity that has a Edit Text named etPerson we would like to enter a value in etPerson and set the entered value equal to globalVar
Why because we want to make a call to a database function in another Activity
Here is the code that makes the call to the DB

    var mApp = MyApplication()
    var intGlobalVar = mApp.globalVar
    println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@ globalINT = "+intGlobalVar)
    var ITEM = db.getItem(intGlobalVar)
    println("%%%%%%%%%%%%%%%%%% ITEM "+ITEM?.id+" name "+ITEM?.name)

And for clarity here is the DB function

    fun getItem(id: Int): Contact? {
    val db = this.writableDatabase
    val selectQuery = "SELECT  * FROM $TABLE_NAME WHERE $colId = ?"
    db.rawQuery(selectQuery, arrayOf(id.toString())).use {
        // .use requires API 16
        if (it.moveToFirst()) {
            val result = Contact(0)
            result.id = it.getInt(it.getColumnIndex(colId))
            result.name = it.getString(it.getColumnIndex(colName))
            return result
        }
    }
    return null
}

So the issue is setting the var globalVar to the value entered in etPerson on the Main Activity
The concept can be accomplished using put and get with intents but that is not our goal here

Our question is how to set the globalVar to the value entered in the Edit Text?

MMG
  • 3,226
  • 5
  • 16
  • 43
Vector
  • 3,066
  • 5
  • 27
  • 54

2 Answers2

35

When your app starts one and only one object of the class MyApplication() will be created, so you don't need:

var mApp = MyApplication()

You can access MyApplication() and all its members from everywhere in your app.

Declare a companion object in MyApplication() and put globalVar's declaration inside:

class MyApplication: Application() {    
    companion object {
        var globalVar = 2
    } 

    override fun onCreate() {
        super.onCreate()
        // initialization code here
    }
}

So in your MainActivity class or elsewhere you can use MyApplication.Companion.globalVar to get or set its value.
Or you can import the globalVar variable in any class like:

import yourpackagename.MyApplication.Companion.globalVar

and refer to it simply globalVar

You also need to declare MyApplication in the manifest:

  <application
        android:name=".MyApplication"
forpas
  • 160,666
  • 10
  • 38
  • 76
  • Thanks that seems to fix the issue We have a ModelClass can we put this companion declaration in the Model? – Vector Oct 16 '18 at 22:32
  • This companion declaration must reside in a class that inherits the Application class – forpas Oct 16 '18 at 22:34
  • we extended the Contact Class it makes the DBHelper a little crazy but we removed the zero from this line val contact = Contact(0) and all is well WE HAVE a question with a bounty at https://stackoverflow.com/questions/52747527/kotlin-recyclerview-data-not-showing if you would like to take a look Thanks – Vector Oct 16 '18 at 22:42
  • I will, but tomorrow. It's late here and I'm off soon – forpas Oct 16 '18 at 22:44
  • Thanks a lot we have been struggling with this for weeks – Vector Oct 16 '18 at 22:57
0

I use this way to send an Intent :

Target activity or class or object (Reciver):

class ReturnActivityIntent private constructor() {
        var data: Intent? = null
        companion object {
            val instance = ReturnActivityIntent()
        }
    }

In the MainActivity (Sender)

val returnActivityIntent: Notifications.ReturnActivityIntent = Notifications.ReturnActivityIntent.instance
        returnActivityIntent.data = Intent(this, MainActivity2::class.java)

so you can change or share variables in this way

Mori
  • 2,653
  • 18
  • 24