0

I am trying to store the String from the Hours and Number for use in a new activity. I have found ways of doing it with intent but do not want the strings sent through to the next activity. Is there any way of me saving my String data and being able to call it to a different activity?

class ChecksStartUp : AppCompatActivity() {


    lateinit var hours: EditText
    lateinit var number: EditText


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        getSupportActionBar()?.hide()
        setContentView(R.layout.activity_Checks_pg1)


        val number = findViewById(R.id.machineFleetNumberId)
        val hours = findViewById(R.id.machineHoursId)
        val dButton = findViewById<RadioButton>(R.id.dButtonId)
        val eButton = findViewById<RadioButton>(R.id.eButtonId)
        val hButton = findViewById<RadioButton>(R.id.hButtonId)
        val proceedButton = findViewById<Button>(R.id.proceedButtonId)

        proceedButton.setOnClickListener {


            if (dButton.isChecked()) {
                val intent = Intent(this, DPage::class.java)
                startActivity(intent)
            }
            if (eButton.isChecked()) {
                val intent = Intent(this, EPage::class.java)
                startActivity(intent)
            }
            if (hButton.isChecked()) {
                val intent = Intent(this, HPage::class.java)
                
            }
        }
    }
}
apt-get_install_skill
  • 2,818
  • 10
  • 27
Dan
  • 1
  • 1
  • You can save to disk and read the save data in the next activity. There are multiple ways to save data. SharedPreferences is simplest for small, primitive types of data like Strings. https://developer.android.com/training/data-storage Whether it makes sense to do this instead of passing them in an intent depends on your use case. – Tenfour04 Aug 19 '22 at 00:55

1 Answers1

0

Alot of different ways to store data. Simplest way is to create singleton class - object. In Android studio create new Kotlin file/class, choose Object. this is short example: object GlobalVariables { var hourses:String?="" var number:Int?= null } access: GlobalVariables.number=3 - set val number =GlobalVariables.number - get You can access this data everywhere you want in your classes, fragments and activites , but remember - when app is terminated data is immediately lost.

Dragan.T
  • 317
  • 1
  • 5