简体   繁体   中英

Move Data from Second Activity to first Activity

I have an Activity which has an empty Text, you can click add item to add text to the Text, so when I try to add the data from second Activity to main back the App won't even launch.
MainActivity

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val textView1: TextView = findViewById(R.id.num1)
        val textView2: TextView = findViewById(R.id.num2)
        val Button: Button = findViewById(R.id.button)
        val intent = intent
        val message: String = intent.getStringExtra(SecondActivity.EXTRA_MESSAGE)
        textView1.setText(message)
        Button.setOnClickListener(){
            val intent: Intent = Intent(this, SecondActivity::class.java)
            startActivity(intent)
        }
    }

SecondActivity:

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_second)
        val item = edit.text.toString()
        button.setOnClickListener(){
            val intent = Intent(this, MainActivity::class.java)
            intent.putExtra(EXTRA_MESSAGE , item)
            startActivity(intent)
        }
    }
    companion object {
        val EXTRA_MESSAGE: String =
            "com.example.android.twoactivities.extra.MESSAGE"
    }

To receive data from Second activity after intending from first activity.
Step 1 : launch Second Activity using Intent with startActivityForResult(Intent intent, int REQUEST_CODE) by doing this, you will receive the data send from second activity

val intent: Intent = Intent(this, SecondActivity::class.java) startActivityForResult(intent, 101)

Step 2 : Open Second Activity and write the below code where you want to finish the Second Activity (ie, where you want to send data to First Activity ) Note : "KEY" and the data-type passing is important to get the result in First Activity

val value : String = "sendBackData"
val intent = Intent()
        resultIntent.putExtra("KEY", value)
        setResult(Activity.RESULT_OK, intent)
        finish()

Step 3 : In First activity. create override method onActivityResult to get the data into First Activity with "KEY"

Note : The key at Second Activity and Key inside onActivityResult of First Activity should be same.

override fun onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 101 && resultCode == RESULT_Ok && data != null){
     val str : String = data.getStringExtra("KEY")
}
}

you need to call

startActivityForResult(intent, request_code) ;

instead of

startActivity(intent);

in order to receive the response in onActivityResult method.

Hi use startActivityForResult(intent, requestcode); and in your SecondActivty just use this

val myintent = Intent()
            resultIntent.putExtra("YourKey", value)
            setResult(Activity.RESULT_OK, myintent)
            finish()

to retrun to your MainActivity.

First of all, from MainActivity to SecondActivity you need to use startActivityForResult()

    val requestCode = 0
    val intent = Intent(this, SecondActivity::class.java)
    intent.putExtra("your key","your value")
    this.startActivityForResult(intent, requestCode)

you declare a request code, an integer, declare the intent like you are doing now, and put your key in the intent put extra. The important part: You need to use startActivityForResult with your intent and the request code.

Now, in the second activity, to get your intent string:

 val string = intent.getStringExtra("your key")

When you are ready to return a value, use:

    val resultCode = 1
    val returnIntent = Intent()
    returnIntent.putExtra("your key","your value")
    setResult(resultCode, intent)
    finish()

Where you create a new intent, you put what you need to put in the intent extras, and you return it with setResult - Remember the resultCode and the requestCode. The finish() is to close the activity you are currently in (only set result won't close the activity)

Finally, on the main activity you need to override this method:

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    if (requestCode == yourRequestCode){
        if (resultCode == yourResultCode){
            //do something
        }
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM