如何将字符串从一个活动传递到另一个活动?

79t*_*97g 1 java string android android-activity

我想知道如何传递和读取来自另一个活动的一个活动中的字符串.我有两个活动.我将它们称为Activity1和Activity2.我在Activity1中调用了一个字符串course.我想在Activity2中读取该字符串.

我试过这样做,但字符串出来了.

public class Activity2 extends Activity1 {

我见过人们使用Intent函数,但我无法弄清楚如何使用它.

有什么建议?谢谢!

Rag*_*dan 7

使用意图传递值.

在你的第一个活动中

 Intent i= new Intent("com.example.secondActivity");
 i.putExtra("key",mystring);
 // for explicit intents 
 // Intent i= new Intent(ActivityName.this,SecondActivity.class);    
 // parameter 1 is the key
 // parameter 2 is the value 
 // your value
 startActivity(i);
Run Code Online (Sandbox Code Playgroud)

在您的第二个活动中检索它.

Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("key");
//get the value based on the key
}
Run Code Online (Sandbox Code Playgroud)

要传递自定义对象,您可以查看此链接

http://www.technotalkative.com/android-send-object-from-one-activity-to-another-activity/