0

i try to get some data from MainActivity and show it in second activity i use putExtra but am still beginner and don't know so much about intents and data in MainActivity i should insert my birth year and month then when i click at the button show me the result ,

class MainActivity : AppCompatActivity() {

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


    }


 override fun onStart() {
        super.onStart()


        getAGE.setOnClickListener {

            @SuppressLint("SetTextI18n")


            val Get_year_input = age_year_input.text.toString().toInt()
            val getCurrentYear = Calendar.getInstance().get(Calendar.YEAR)
            val finish_year_input = Get_year_input - getCurrentYear
            val Get_month_input = age_month_input.text.toString().toInt()
            val getCurrentMonth = Calendar.getInstance().get(Calendar.MONTH)
            val finish_month_input = age_month_input.text.toString().toInt() - getCurrentMonth

            // Send Data


            var i_month = Intent()
            i_month.putExtra("Month", finish_month_input)
            setResult(Activity.RESULT_OK)
            finish()

            var getintent = Intent("ahsb3omrk.show.result")


        }
    }

}

Second Activity :

class Second : AppCompatActivity() {

    @SuppressLint("SetTextI18n")

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


    }


override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {


    if (requestCode == 4 && resultCode == Activity.RESULT_OK){

        var showintent = Intent(data!!.extras.getString("Month"))
        viewMonth.text = showintent.toString()
        viewDay.text = "Hello"
    }
}


}

2 Answers2

1

setResult() and onActivityResult() have nothing to do with this case.
You pass the data to the intent:

val i_month = Intent(applicationContext, Second::class.java)
i_month.putExtra("Month", finish_month_input)
startActivity(i_month)

and in Second activity's onCreate():

val finishMonthInput = intent.getIntExtra("Month", -1)

now in the finishMonthInput variable you will have the integer value passed, or -1 if there was no value passed.

Remove these lines from your code:

setResult(Activity.RESULT_OK)
............................
var getintent = Intent("ahsb3omrk.show.result")

and the whole onActivityResult() method from the Second class.

forpas
  • 160,666
  • 10
  • 38
  • 76
  • and to show data but still doesn't work – Khadeja Alhaaj Dec 08 '18 at 16:04
  • What doesn't work? – forpas Dec 08 '18 at 16:04
  • no data showing – Khadeja Alhaaj Dec 08 '18 at 16:06
  • Check in `onCreate()` after this line `val finishMonthInput = intent.getIntExtra("Month", -1)` the value of `finishMonthInput` if it is correct. If it is then you passed the value correctly, if not then you did something wrong. Also put this line `viewMonth.text = finishMonthInput.toString()` in `onCreate()` after you get the value of `finishMonthInput`. – forpas Dec 08 '18 at 16:11
  • it's work but why you used -1 – Khadeja Alhaaj Dec 08 '18 at 16:14
  • It is the default value if no value is passed. So you check if it is -1 then there ws no extra value passed. I could use any other value but since we're dealing with a month value I thought that a negative number is a good choice. – forpas Dec 08 '18 at 16:21
0

To pass data between activities, start the target activity using startActivity(Intent). With the Intent object having the data you want to send (use the putExtra() method).

In the second activity, access the data from the intent argument in the onCreate() method

The developer website has an article on how to do all this.

Eddy
  • 76
  • 1
  • 7