Search code examples
androidkotlinandroid-activity

While sending data through multiple activities, all the variables get value from the last activity


I'm currently working on a simple app which counts the required amount of calories to bulk, cut or maintain body mass. The app consists of seven activities. Six of them are devoted to apply a specified parameter (like for example age, height etc.). Those parameters are saved on a file "Variables.kt". The seventh activity has to display all the implemented parameters and display the amount of calories needed to fulfill the desired plan.

The problem I encountered: It turns out that every of the parameters gets the data implemented in the sixth activity.

Here's the examplary code:

FirstActivity.kt :

//        Variables
        val intentGoToSecondActivity = Intent(this@FirstActivity, SecondActivity::class.java)

//        Main part of the code
        buttonFirstActivity.setOnClickListener {
            intentGoToSecondActivity.putExtra(Variables.firstMessage, "First message")
            startActivity(intentGoToSecondActivity)
        }

SecondActivity.kt :

//        Variables
        val getData = intent.extras
        val intentGoToThirdActivity = Intent(this@SecondActivity, ThirdActivity::class.java)


//        Main part of the code
        buttonSecondActivity.setOnClickListener {
            intentGoToThirdActivity.putExtra(Variables.firstMessage, intent.getStringExtra(Variables.firstMessage))
            intentGoToThirdActivity.putExtra(Variables.secondMessage, "Second message")
            startActivity(intentGoToThirdActivity)
        }

ThirdActivity.kt (let's say that it's the seventh activity which displays the implemented data

//        Variables
        val getData = intent.extras


//        Main part of the code
        buttonThirdActivity.setOnClickListener {

//            Getting data from Variables.kt
            val firstMessage = getData?.getString(Variables.firstMessage)
            val secondMessage = getData?.getString(Variables.secondMessage)


//            Displaying gotten data
            displayData.text = firstMessage + "\n" + secondMessage

        }

When I click ButtonThirdActivity.kt it displays "Second message Second message" instead of "First message Second message". What's wrong with the code provided by me?


Solution

  • To solve the problem where the parameters are not getting the implemented data correctly in the seventh activity, you can consider using the following approach:

    1. Instead of saving the parameters in a separate file like "Variables.kt", you can pass the data between activities using Intent extras or as arguments when starting new activities.

    2. In each activity where the user inputs a parameter (age, height, etc.), retrieve the input data and pass it to the next activity when starting it. For example, when moving from the fifth activity to the sixth activity, pass the data from the fifth activity to the sixth activity.

    3. In the seventh activity, retrieve the passed data from the previous activities and calculate the required amount of calories based on the desired plan.

    Here's an example of how you can implement this solution:

    In the fifth activity:

    // Retrieve the input data
    val age = ageEditText.text.toString().toInt()
    
    // Pass the data to the next activity
    val intent = Intent(this, SixthActivity::class.java)
    intent.putExtra("age", age)
    startActivity(intent)
    

    In the sixth activity:

    // Retrieve the input data
    val age = intent.getIntExtra("age", 0)
    // Repeat the same for other parameters
    
    // Pass the data to the next activity
    val intent = Intent(this, SeventhActivity::class.java)
    intent.putExtra("age", age)
    // Repeat the same for other parameters
    startActivity(intent)
    

    In the seventh activity:

    // Retrieve the passed data
    val age = intent.getIntExtra("age", 0)
    // Repeat the same for other parameters
    
    // Calculate the required amount of calories based on the desired plan
    // Display the calculated calories in the UI
    

    By passing the data between activities using Intent extras, you ensure that the correct data is available in the seventh activity to calculate the required amount of calories.