lxg95
lxg95

Reputation: 573

How and where do i declare a global Variable for saving Data

I am trying to build my very first Kotlin App where I save my private Hourly Contingent. For example, if I study 2 hours, I add 2 hours to it. If I play video games for 30 Minutes I subtract 2 hours, so that I'm allowed to play video games for 15 minutes for every hour of studying. I am saving my Data with sharedPreferences and I get this Data from my TextView. When I start the Application, I just load the Data to my TextView. So while running the App, the TextView saves the hours and minutes I already gained:

//Saves the TextView from the Main Activity (Probably bad practice but its my first Kotlin APP and i will improve later)
private fun saveData(){
    val hcString = findViewById(R.id.hourly_contingent) as TextView
    val hcStrArr = hcString.text.split(":").toTypedArray()
    val hcIntArr = arrayOf(hcStrArr[0].toInt(), hcStrArr[1].toInt())

    val sharedPreferences = getSharedPreferences("hourly_contingent", Context.MODE_PRIVATE);
    val editor = sharedPreferences.edit();
    editor.apply(){
        putInt("hc_hours", hcIntArr[0])
        putInt("hc_minutes", hcIntArr[1])
    }.apply()

    Toast.makeText(this, "Data saved", Toast.LENGTH_SHORT).show()
}

//Puts the saved Data to the hourly_contingent String
private fun loadData(){
    val sharedPreferences = getSharedPreferences("hourly_contingent", Context.MODE_PRIVATE);
    val hcHours = sharedPreferences.getInt("hc_hours", 0)
    val hcMinutes = sharedPreferences.getInt("hc_minutes", 0)

    val hourly_contingent = findViewById(R.id.hourly_contingent) as TextView
    val hcString = hcHours.toString() + ":" + hcMinutes.toString()
    hourly_contingent.text = hcString
}

I know this is very bad practice but I don't know how to it better. Should I use a global variable? Where and how do I declare that? Should I create a new File MyApplication.kt and in there a global class like that:

public class Global : Application() {
  open var homeAPIResponse: String = "defaultValue"
}

A little help would be really nice!

Upvotes: 1

Views: 1018

Answers (1)

MiqeyM
MiqeyM

Reputation: 51

First thing i would advise you is to read about basic design patterns for android. Model-View-Controller and Model-View-ViewModel. They really can help you sort out data scope problems and show good practices on presenting data on android. (https://developer.android.com/jetpack/guide)

There are a lot of amazing basics android courses on udacity, made by android devs - all of them, free. They will definitely clarify most of the problems you described here. How you should store data or how should you prepare it for user to display.

So to somehow answer your question: Right now your piece of code is called from MainActivity. Everything your varaibles store within Activity class will be gone after the Activity is destroyed. It stems from the lifecycle of an activity (another good topic to read about). To persist some data, even after app is closed (i.e. activity is destroyed), you either can use SharedPreferences (perfect for small data, even better for data related to UI configuration), file storage or database. Last one is pretty easy by means of Room library, but maybe overkill for storing one float value. I hope this answers your question. Reading link - on data persistency: (https://developer.android.com/training/data-storage)

Upvotes: 2

Related Questions