简体   繁体   中英

Passing values from one activity to another activiity in android studio

I am fairly new to android and I am confused right now about two things that I have noticed and read online. When I start a new intent / activity, it seems that I am allowed to go back to the previous activity with the back button which means two things to me.

1) The previous activity is not destroyed and kept alive somewhere or

2) it's destroyed and every time I click the back button it creates a new intent of the previous activity

Now, here is my problem, according to all the guides I've seen so far, in order to send data from one activity to another activity I have to create a new intent and use the putextra function . My question is whether this process also works for the reverse when I want to send data from the newly created activity back to the previous activity.

Here is a portion of the relevant code :

Home screen code:

public void CreateNewCueCardSet(View view){
    // Create a new page/activity when button is clicked
    Intent NewCueCardPage = new Intent(this, NewCueCardActivity.class);
    startActivity(NewCueCardPage);
}

//NewCueCardActivity.class

public void createSet(View view){
    // this will save text input from user on the activity 
    EditText setType = (EditText) findViewById(R.id.editText);
    EditText setName = (EditText) findViewById(R.id.editText2);
    //How do I send these 2 variable's data back to the home screen? 
}

I am thinking that I just create a new intent of the home screen and send it, but what if the home screen is not destroyed and kept somewhere in memory. What do I do in that case to gain access to it?

It sound like what you might be looking for is startActivityForResult .

Here's some good info on the method: How to manage `startActivityForResult` on Android?

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