SharedPreferences not working when Activity is started from somewhere else?

Viewed 48

I have a ThreadActivity with two functions, saveContacts and loadContacts. They both use sharedpreferences and Gson to save an ArrayList consisting of Objects called SimpleContacts. Somehow it cannot retrieve data from sharedpreferences once I start the Activity from somewhere else. (I tried loading instantly after saving and that works, but not if I close the Activity and re-open it)

The save function:

private fun saveContact() {
        val gson = Gson()
        val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(applicationContext)
        try {
            val editor = sharedPreferences.edit()
            val json = gson.toJson(participants)
            editor.putString(threadId.toString()+"_Contact", json)
            editor.apply()
        } catch(e: Exception) {
            e.printStackTrace()
        }
}

The load function:

private fun loadContact() {
        val gson = Gson()
        val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(applicationContext)
        val type = object : TypeToken<ArrayList<SimpleContact?>?>() {}.type
        try {
            val json = sharedPreferences.getString(threadId.toString()+"_Contact", "")
            participants = gson.fromJson(json, type)
        } catch(e: Exception) {
            e.printStackTrace()
        }
}

I have 2 Activities that can open this ThreadActivity, if I start it from the same one, it all works perfectly fine. But when I use the other Activity to start it, the sharedPrefs are empty.

Launch Activity that works (I don't know if its because its the way the Intent is build so I will write them both here):

private fun launchThreadActivity(phoneNumber: String, name: String) {
        hideKeyboard()
        val text = intent.getStringExtra(Intent.EXTRA_TEXT) ?: ""
        val numbers = phoneNumber.split(";").toSet()
        val number = if (numbers.size == 1) phoneNumber else Gson().toJson(numbers)
        Intent(this, ThreadActivity::class.java).apply {
            putExtra(THREAD_ID, getThreadId(numbers))
            putExtra(THREAD_TITLE, name)
            putExtra(THREAD_TEXT, text)
            putExtra(THREAD_NUMBER, number)

            if (intent.action == Intent.ACTION_SEND && intent.extras?.containsKey(Intent.EXTRA_STREAM) == true) {
                val uri = intent.getParcelableExtra<Uri>(Intent.EXTRA_STREAM)
                putExtra(THREAD_ATTACHMENT_URI, uri?.toString())
            } else if (intent.action == Intent.ACTION_SEND_MULTIPLE && intent.extras?.containsKey(Intent.EXTRA_STREAM) == true) {
                val uris = intent.getParcelableArrayListExtra<Uri>(Intent.EXTRA_STREAM)
                putExtra(THREAD_ATTACHMENT_URIS, uris)
            }

            startActivity(this)
        }
}

Start Activity that does not work:

Intent(this, ThreadActivity::class.java).apply {
                        putExtra(THREAD_ID, (it as Conversation).threadId)
                        putExtra(THREAD_TITLE, it.title)
                        putExtra("fromMain", true)
                        startActivity(this)
}
1 Answers

Nevermind, it was my mistake. When saveContact was called the threadId was not initialized yet. So basically the keys were always different.

Related