0

Tried to create another activity and put same code in it but still had "null" in result.Had problem only with one specific kotlin Activity. Also google it and cant find answer. with rest activities no such problem with same Intent put extra code.

**Noticed that if I set activity startActivity from 'A' to 'C' I can get Intent with same code. But If A>B>C than on 'C' activity I get null from 'A' activity **

class GenderActivity : AppCompatActivity() {

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

        val btn: Button = findViewById(R.id.btnNextGender)
        val radioG: RadioGroup = findViewById(R.id.radioGroupD)




        btn.setOnClickListener {
            val i = Intent(this, GoalsActivity::class.java)
            i.putExtra("sex", "some text")
            startActivity(i)
           

        }
    }


private lateinit var database: DatabaseReference

class QuizActivity2 : AppCompatActivity() {

@SuppressLint("MissingInflatedId")
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_quiz2)





    val btnAddUser = findViewById<Button>(R.id.buttonAddUser)

    val muscles = intent.getStringExtra("muscles").toString()
    val gender:String = intent.getStringExtra("sex").toString()

    val goal = intent.getStringExtra("goal")


    val textOn:TextView = findViewById(R.id.textViewQst2)


    btnAddUser.setOnClickListener{
        val userName = findViewById<EditText>(R.id.inputUserName).text.toString()
        val userAge = findViewById<EditText>(R.id.inputUserAge).text.toString()



        if(userAge.isEmpty()||userName.isEmpty()){
            Toast.makeText(
                this,
                "заповни всі поля",
                Toast.LENGTH_SHORT
            ).show()
        }
        if(userAge.isNotEmpty()&&userName.isNotEmpty()){
        database = FirebaseDatabase.getInstance().getReference("Users")


        val userId = database.push().key!!
        data class User(val firstName:String? = null,val userGender:String? = null,val age:String? = null,val uId:String? = null,val muscles:String? = null,val goals:String? = null)
        val user = User(userName,gender,userAge,userId,muscles,goal)
            textOn.text = user.toString()

        database.child(userName).setValue(user).addOnSuccessListener {
            findViewById<EditText>(R.id.inputUserName).text.clear()
            findViewById<EditText>(R.id.inputUserAge).text.clear()
        }
    }



}


}

}


Max
  • 3
  • 2

2 Answers2

0

Try this intent.extras?.getString("sex")if this does not work then, clean your build or do the invalidate cache and restart Android studio, Looks like there is no issue with the code. refer this thread if you need to. here

Lalit Behera
  • 534
  • 8
  • 18
0

When the intent extra is passed from activity A -> B that value is for B only. If you want to pass it to C you’ll need to add it to the intent that calls Activity C

Getting intent from A in B:

  val gender:String = intent.getStringExtra("sex").toString()

Then passing it again in Activity B when calling intent for activity C:

val i = Intent(this, ActivityC::class.java)
        i.putExtra("sex",gender)
        startActivity(i)
Wisdom
  • 40
  • 6