1

I am currently trying to make a TextView show whatever date my TimePicker returns. It works, but if I go into another fragment and back, restart the app, etc, the text reverts to the default I set. Does anyone know how I could make the text I'm setting persistent? Here is the code that I am using.

view.timeButton.setOnClickListener {
            val cal = Calendar.getInstance()
            val timeSetListener = TimePickerDialog.OnTimeSetListener { timePicker, hour, minute ->
                cal.set(Calendar.HOUR_OF_DAY, hour)
                cal.set(Calendar.MINUTE, minute)

                alarmText.text = ("Texts at " + SimpleDateFormat("HH:mm").format(cal.time))
            }
            TimePickerDialog(
                context,
                timeSetListener,
                cal.get(Calendar.HOUR_OF_DAY),
                cal.get(Calendar.MINUTE),
                false
            ).show()
        }
mark
  • 85
  • 4
  • 16
  • you need to save/restore your values to be persistent when the activity pauses and resumes https://stackoverflow.com/questions/37152601/what-is-savedinstancestate – ecle Nov 07 '20 at 03:45
  • Do you want to show the time in each moment or just show a specific time? – Ramin eghbalian Nov 07 '20 at 05:33
  • @Ramineghbalian I'm trying to make the TextView show whatever time the timepickerdialog returns – mark Nov 07 '20 at 05:55

1 Answers1

1

You can use SharedPreference to save it persistently

view.timeButton.setOnClickListener {
            val cal = Calendar.getInstance()
            val timeSetListener = TimePickerDialog.OnTimeSetListener { timePicker, hour, minute ->
                cal.set(Calendar.HOUR_OF_DAY, hour)
                cal.set(Calendar.MINUTE, minute)

                val simpleDateFormat = SimpleDateFormat("HH:mm")
                val date = simpleDateFormat.format(cal.time)
                alarmText.text = ("Texts at " + date)

                val preferences: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
                preferences.edit().putString("mydate", date).apply();
                
            }
            TimePickerDialog(
                context,
                timeSetListener,
                cal.get(Calendar.HOUR_OF_DAY),
                cal.get(Calendar.MINUTE),
                false
            ).show()
        }

And in your app's onCreate retrieve the stored value

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
        try {
            val preferences: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
            val simpleDateFormat = SimpleDateFormat("HH:mm")
            val date: Date = simpleDateFormat.parse(preferences.getString("mydate", ""))
            alarmText.text = ("Texts at " + date)
        }
        catch (e: ParseException) {
        }
    }
Zain
  • 37,492
  • 7
  • 60
  • 84
  • Hello! Thank you for the response, but when I try this, my app turns white completely for some reason and nothing shows up. I don't get any errors in LogCat, but all my buttons, TextViews, navbars, etc just all disappear. – mark Nov 07 '20 at 04:35
  • 1
    Hello! excuse me I just was overriding a wrong `onCreate` version, please use the version of the only `bundle` argument .. – Zain Nov 07 '20 at 04:39
  • sorry for keeping this so long, but with only the `bundle` argument, my `persistentState` is an unresolved reference. Do you know how I could solve this? Thanks – mark Nov 07 '20 at 04:53
  • 1
    np, can you share a bit more of `onCreate`, do you use `persistentState` in some context? – Zain Nov 07 '20 at 04:59
  • I only mention persistentState in my `super.onCreate(savedInstanceState, persistentState)` if that's what you mean – mark Nov 07 '20 at 05:00
  • 1
    my bad, please replace it with `super.onCreate(savedInstanceState)` with the single arg `onCreate` .. hopefully it works now – Zain Nov 07 '20 at 05:04
  • Like this? ```kotlin override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) ``` I tried doing this but my app crashed. Logcat showed me this error: `java.lang.NullPointerException: alarmText must not be null` Again, sorry for the long comment thread – mark Nov 07 '20 at 05:10
  • 1
    I guess `alarmText` is the `TextView`; Not sure how you defined it, something like `val alarmText: TextView = findViewById(R.id.my_textview) as TextView` – Zain Nov 07 '20 at 05:16