Search code examples
androidkotlinandroid-intentandroid-activity

Android Kotlin Starting Activity From Utils Class Results In SavedInstanceState Being Null


Just to clarify beforehand, I have searched extensively in SO for similar question to find an answer to my question.

I will reference the following (to name a few sources) :

I have a MainActivity and four other different activities, let's call them A through D.

I also have a Utilities class which sets on click listeners to image buttons found in activities A through D.

These listeners then open a new activity, E.

For some reason, in the onCreate method for activity E, savedInstanceState is always null. I have tried setting the listener from the MainActivity, but to no avail. I have also passed the context from the MainActivity (instead of using the scroll view's), but that had not effect either

Below is a snippet of the code.

Utilities.kt

class Utilities {

companion object {
    /...

    fun setTooltipsAndListeners(scrollView: ScrollView) {
        val buttons: ArrayList<View> = scrollView.touchables
        for (button in buttons) {
            val tooltipText = button.contentDescription
            if (tooltipText != null) {
                TooltipCompat.setTooltipText(button, tooltipText)
            }

            button.setOnClickListener(object : View.OnClickListener {
                override fun onClick(v: View?) {

                    val tag: String? = v?.tag as? String
                    val intent = Intent(scrollView.context, ActivityE::class.java)
                    intent.putExtra("symbol_name", tooltipText)
                    intent.putExtra("symbol_image", tag)

                    scrollView.context.startActivity(intent)

                }
            })

        }
    }

   /...
}

ActivityE.kt

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.layout_name)
    setDataToUI(savedInstanceState)
}

private fun setDataToUI(savedInstanceState: Bundle?) {

    if (savedInstanceState == null) {
        Log.d("TAG", "savedInstanceState IS NULL")
        return
    }

  /... Inner logic that is not relevant
}

Solution

  • Instead of savedInstanceState you need to use getIntent(). So your code will change like this:

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.layout_name)
        setDataToUI(intent)
    }
    
    private fun setDataToUI(intent: Intent?) {
    
        if (intent.getStringExtra("symbol_name") == null) {
            Log.d("TAG", "No data passed for symbol_name")
            return
        }
        // Do the same for other strings. Or alternatively, you can pass Bundle 
        // data from your Utility and retrieve the bundle through the intent as well
    }
    

    savedInstanceState is used to store data when the state of the activity is changed, but you are passing data through an Intent in your utility class