如何从以前的活动中获得新活动的结果导致android?

时间:2012-09-27 16:55:50

标签: android android-intent

我有两个活动说X和y。在x中有edittext n 6 radiobutton,如果用户点击按钮,则根据来自edittext n radiobutton的输入从数据库中检索该值。值应显示在下一个活动y中。 yu请帮助提供片段...提前谢谢

3 个答案:

答案 0 :(得分:0)

您应该将值放入一个包中,并将该包传递给开始下一个活动的意图。示例代码就是这个问题的答案:Passing a Bundle on startActivity()?

答案 1 :(得分:0)

将您要发送的数据与您要呼叫的意图绑定到下一个活动。

Intent i = new Intent(this, YourNextClass.class);
i.putExtra("yourKey", "yourKeyValue");
startActivity(i);

在YourNextClass活动中,您可以使用

获取传递的数据
    Bundle extras = getIntent().getExtras();
    if (extras != null) {
    String data = extras.getString("yourKey");
    }

答案 2 :(得分:0)

您可以使用Bundle或Intent轻松地将数据从一个活动传递到另一个活动。

让我们看一下使用Bundle的以下示例:

//creating an intent to call the next activity
Intent i = new Intent("com.example.NextActivity");
Bundle b = new Bundle();

//This is where we put the data, you can basically pass any 
//type of data, whether its string, int, array, object
//In this example we put a string
//The param would be a Key and Value, Key would be "Name"
//value would be "John"
b.putString("Name", "John");

//we put the bundle to the Intent
i.putExtra(b);

startActivity(i, 0);

在“NextActivity”中,您可以使用以下代码检索数据:

Bundle b = getIntent().getExtra();
//you retrieve the data using the Key, which is "Name" in our case
String data = b.getString("Name");

如何仅使用Intent传输数据。让我们看一下示例

Intent i = new Intent("com.example.NextActivity");
int highestScore = 405;
i.putExtra("score", highestScore);

在“NextActivity”中,您可以检索数据:

int highestScore = getIntent().getIntExtra("score");

现在你会问我,看看Intent和Bundle之间的区别是什么 他们完全一样。

答案是肯定的,他们都做了完全相同的事情。但是如果你想传输很多数据,变量,大数组,你需要使用Bundle,因为它们有更多的方法来传输大量数据。(例如,如果你只传递一两个变量,那么只需要使用Intent。 / p>

相关问题