如何将数据从一个 activity 传输到 Android 中的另一个?

How do I transfer data from one activity to another in Android?

我想将我的 "result" 数据从我的第一个(主要)活动转移到 我的 Custaddress activity,其中包含客户详细信息的编辑文本,然后将其发送到电子邮件。 email/edit 文本工作得很好——但我想 将 "result.toString" 添加到电子邮件正文字符串中。如何将 "result" 转移到第二个 activity?我相信它与 arg 有关? 这是我第一个 activity..

的代码
DecimalFormat decimalFormat = new DecimalFormat(COMMA_SEPERATED);
          result.append("\nTotal: £"+decimalFormat.format(totalamount));
     AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
      alertDialogBuilder.setMessage(result.toString());
      alertDialogBuilder.setTitle("YOUR ORDER");
      alertDialogBuilder.setPositiveButton("Accept", 
      new DialogInterface.OnClickListener() {

         @Override
         public void onClick(DialogInterface arg0, int arg1) {
            //do what you want to do if user clicks ok
             //Intent intent = new Intent(context, Custaddress.class);
            // startActivity(intent);
             Intent custaddress = new Intent(getApplicationContext(),com.example.frytest.Custaddress.class);
             startActivity(custaddress);
         }
      });
      alertDialogBuilder.setNegativeButton("Decline", 
      new DialogInterface.OnClickListener() {

         @Override
         public void onClick(DialogInterface dialog, int which) {
            //do what you want to do if user clicks cancel.
         }
      });

      AlertDialog alertDialog = alertDialogBuilder.create();
      alertDialog.show();

你应该使用意图 (click here) :

Intent intent = new Intent(getBaseContext(), CustAdrresActivity.class);
intent.putExtra("text", mytext);
startActivity(intent);

你只需要替换你的台词:

Intent custaddress = new Intent(getApplicationContext(),com.example.frytest.Custaddress.class);
startActivity(custaddress);

这三行:

Intent custaddress = new Intent(getApplicationContext(),com.example.frytest.Custaddress.class);
custaddress.putExtra("result", result.toString());
startActivity(custaddress);

然后,当您打开新的 activity(在您的情况下是 Custaddress Activity)时,您应该执行以下操作以获取 result

Bundle extras = getIntent().getExtras();
if (extras != null) {
    String value = extras.getString("result");
}    

您应该在 startActivity(intent) 方法中传递的 Intent 对象中添加 Extra。 例子

String value = "String i want to send to next activity"
Intent intent = new Intent(getApplicationContext(),com.example.frytest.Custaddress.class);
intent.putExtra("KEY", value);
startActivity(intent);

在Custaddress.javaActivityclass中你需要从你在onCreate(Bundle bundle)方法中作为参数获取的Bundle对象获取数据

Bundle extras = getIntent().getExtras();
if (extras != null) {
    // get data via the key
    String valueFromPreviousActivity = extras.getString("KEY");

    if(valueFromPreviousActivity != null){
         // do something with the data
    }
}

写在activity那个传递数据

Intent custaddress = new Intent(getApplicationContext(),com.example.frytest.Custaddress.class);
custaddress.putExtra("key",value);
startActivity(custaddress);

在activity中写下面的代码,捕获数据

Intent intent=getIntent();
String mString=intent.getStringExtra("key");

希望对您有所帮助

查看此官方文档 Starting Another Activity

通过下面的代码,我们可以在活动之间发送值

在parentactivity

中使用下面的代码
Intent myintent=new Intent(Info.this, GraphDiag.class).putExtra("<StringName>", value);
startActivity(myintent);

在childactivity

中使用下面的代码
String s= getIntent().getStringExtra(<StringName>);