Home > front end >  how to save data from textview when going to previous activity kotlin
how to save data from textview when going to previous activity kotlin

Time:12-29

So I have a child activity that I press two buttons, one to increment and one to decrement. I want to save this number when I go back to the previous activity. However, I am stuck here. I tried using shared preference, however that seems to work for main to secondary activity. I tried using Activity Result and that seems way above me right now. I want my value in the textView to stay until I press a button to reset the whole thing.

This is the parent activity.

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity

class TallBoys : AppCompatActivity() {

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


    val btn1: Button = findViewById(R.id.button1)
    val btn2: Button = findViewById(R.id.button2)
    val btn3: Button = findViewById(R.id.button3)
    val btn4: Button = findViewById(R.id.button4)
    val btn5: Button = findViewById(R.id.button5)
    val btn6: Button = findViewById(R.id.button6)
    val btn7: Button = findViewById(R.id.button7)



    btn1.setOnClickListener {
        val intent = Intent(this, TallBoysNumbers::class.java)

    }

    btn2.setOnClickListener {
        val intent = Intent(this, TallBoysNumbers::class.java)
        startActivity(intent)
    }

    btn3.setOnClickListener {
        val intent = Intent(this, TallBoysNumbers::class.java)
        startActivity(intent)
    }
    btn4.setOnClickListener {
        val intent = Intent(this, TallBoysNumbers::class.java)
        startActivity(intent)
    }
    btn5.setOnClickListener {
        val intent = Intent(this, TallBoysNumbers::class.java)
        startActivity(intent)
    }
    btn6.setOnClickListener {
        val intent = Intent(this, TallBoysNumbers::class.java)
        startActivity(intent)
    }
    btn7.setOnClickListener {
        val intent = Intent(this, TallBoysNumbers::class.java)
        startActivity(intent)
    }


}

}

This is the child activity

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity


class TallBoysNumbers : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_tall_boys_numbers)





    val confirmBtn: Button = findViewById(R.id.confirm_button)
    val plusBtn: Button = findViewById(R.id.plus)
    val textView = findViewById<TextView>(R.id.numbers)
    val negBtn: Button = findViewById(R.id.negative)
    var count = 0



    plusBtn.setOnClickListener {
        count  
        textView.text = count.toString()
        if (count >= 8) {
            plusBtn.isEnabled = false
            negBtn.isEnabled = true
        }

    }


    negBtn.setOnClickListener {
        count--
        textView.text = count.toString()
        if (count <= 0) {
            negBtn.isEnabled = false
            plusBtn.isEnabled = true
        }
    }




        confirmBtn.setOnClickListener {
            finish()
        }
    }
}
    

CodePudding user response:

You are calling "finish" method. This will finish TallBoysNumbers activity without saving any data. Do not use SharedPreferences for that. What you'll need is "startActivityForResult".

This will allow you to save the data before calling "finish" AND getting it from the caller activity. Take a look here:

https://developer.android.com/training/basics/intents/result

CodePudding user response:

with using shared pref I hope it will come to you

class dataShared() for saving count

class DataShared(
      context: Context,
    userSharedPrefName:String="USER_SHARED_PREF_NAME"
    ) {
    private val sharedPreferences: SharedPreferences = 
     context.getSharedPreferences(
        userSharedPrefName, Context.MODE_PRIVATE
    )



    fun getCount(): Int {
        return (sharedPreferences.getInt("count", 0))
    }

    fun setCount(value: Int) {
        val editor = sharedPreferences.edit()
        editor.putInt("count", value)
        editor.apply()
    }


}

plusBtn and negBtn

val data=DataShared(this)

plusBtn.setOnClickListener {
    if (data.getCount()<7) {
        data.setCount(data.getCount()   1)
        textView.text = data.getCount().toString()
    }
}

negBtn.setOnClickListener {
    if (data.getCount()>=1){
        data.setCount(data.getCount()-1)
        textView.text=data.getCount().toString()
    }
}
  • Related