简体   繁体   English

在Android Studio中将值从一项活动传递到另一项活动

[英]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. 我对android来说还很陌生,现在我对在网上注意到和注意到的两件事感到困惑。 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 1)先前的活动没有被破坏并且在某个地方或其他地方保持生命

2) it's destroyed and every time I click the back button it creates a new intent of the previous activity 2)它被销毁了,每当我单击“后退”按钮时,它都会创建上一个活动的新意图

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 . 现在,这是我的问题,根据到目前为止所看到的所有指南,为了将数据从一个活动发送到另一个活动,我必须创建一个新的 Intent并使用putextra函数。 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 //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 . 听起来您可能正在寻找的是startActivityForResult

Here's some good info on the method: How to manage `startActivityForResult` on Android? 以下是有关此方法的一些很好的信息: 如何在Android上管理`startActivityForResult`?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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