0

I made a functions.kt file for global variables and I made this:

import android.app.Application

class variable : Application() {
    var currentLesson: String? = null
}

After that, I used it in main.kt like so:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        button: Button = findViewById(R.id.button1)
        var functions = variable()
        var currentLesson = functions.currentLesson

        button.onClickListener {
            currentLesson = "text"
        }
    }

    override fun onBackPressed() {
        someview: View =
        findViewById(R.id.view1)
        var functions = variable()
        var currentLesson = functions.currentLesson

        if (currentLesson == "text") {
             someview.visibility = View.VISIBLE
        }
    }

}

In onBackPressed() it's always null. But not in onCreate(). Where is the problem?

1 Answer 1

2

Every time you call variable() you are creating a new instance of the class variable so it has its own memory and state that it's holding.

Incidentally, you should not be subclassing Application to create this class, since your simple data holder is not an Application!

If you want a class to hold some shared state for the whole app, that's commonly called a singleton (a class with only one instance), and you can easily create one in Kotlin using an object instead of class.

object Variable {
    var currentLesson: String? = null
}

Then when you use it in your Activity, you can call it directly with Variable.currentLesson without having to create an instance of the class.

Alternatively, you can put the variable outside of any class, and it will be a global property that can be accessed from anywhere. In my opinion, that's kind of an ugly solution because it pollutes the namespace.

Note, you should be careful about what you store in global variables like this. It is not good practice to put large, memory-hungry objects in a global variable that will cause that memory to be used for longer than necessary.

Also, it is convention to make class names always start with a capital letter. It will make your code much easier to read and understand, especially in languages like Kotlin which omit the new keyword used for constructor calls.

Sign up to request clarification or add additional context in comments.

5 Comments

Should i write that to main.kt and outside of onCreate?
You can put it where you need it. If you need to access it from many different classes, it is natural to put it in its own file. If you are only using it inside your MainActivity, I would say it probably shouldn't be a global variable in the first place. Just add a property inside MainActivity.
I maybe use it in many activities so. Also if its kill memory how to manage and decrease usage
It depends what you're doing what the best design is. Is this text coming from a web server so you can just refetch it when it's needed? Or is it created by the user so you need to save it? Does it need to persist until the next time the app is closed and re-opened? etc.
It not come from webserver or anything like that and not need to persist every launch of app it just a saves location like user clicked open math lessons and it will save "math" text so if user want go back it will be in math layout. Think of it like a bookmark. you put a bookmark somewhere and you started reading if you want to go back to a certain place you can use the bookmark

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.