my kotlin project has 2 activitis (main , second called : AddPlayer) main activity has a button called "Add a player" that it starts second activity . in second activity i have 2 edit text to get a number and a text from user . after press submit button in second activity , in the main activity i want add these values to my mutablelist but idk how to do it ( i think it crashed because main activity uses those variables before getting values from user) plz guide me .
my activity code :
class MainActivity : AppCompatActivity() {
private var enteredPlayerNumber = 0
private var enteredPlayerName: String=""
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val myRecyclerView = findViewById<RecyclerView>(R.id.recyclerView)
val myViewModel = ViewModel()
val btnAdd = findViewById<Button>(R.id.btnAdd)
myRecyclerView.layoutManager = LinearLayoutManager(this)
myViewModel.getUserData()
btnAdd.setOnClickListener {
val myIntent = Intent(this, AddPlayerActivity::class.java)
startActivity(myIntent)
}
myViewModel.myList.observe(this) {
myRecyclerView.adapter = MyAdapter(it)
}
}}
and my second activity :
class AddPlayerActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_add_player)
val numberAddPlayer = findViewById<EditText>(R.id.etNumberAdd2)
val nameAddPlayer = findViewById<EditText>(R.id.etNameAdd2)
val btn = findViewById<Button>(R.id.btnSubmitAdd2)
btn.setOnClickListener {
val myIntent=Intent(this,MainActivity::class.java)
myIntent.putExtra("USERNAME", nameAddPlayer.text)
myIntent.putExtra("USER_NUMBER", numberAddPlayer.text)
startActivity(myIntent)
}
}}