1

I want to transfer my "result" data from my first (Main) acitivity to my Custaddress activity, which has edit texts for customer details, and then this is sent to an email. The email/edit texts work perfectly - but I want to add in "result.toString" into email body string. How do I transfer "result" to the second activity? I believe its something to do with arg? Here's my code from first 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();
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user1641906
  • 117
  • 1
  • 2
  • 14
  • [http://stackoverflow.com/questions/11303887/how-can-i-transfer-the-data-between-two-activities-in-android](http://stackoverflow.com/questions/11303887/how-can-i-transfer-the-data-between-two-activities-in-android) – M D May 22 '15 at 12:50
  • 1
    possible duplicate of [How to send an object from one Android Activity to another using Intents?](http://stackoverflow.com/questions/2139134/how-to-send-an-object-from-one-android-activity-to-another-using-intents) – 2Dee May 22 '15 at 12:52
  • which email client you are using? or you have your own email composer window? – Paresh Mayani May 22 '15 at 12:56

6 Answers6

2

Write it in activity that passing data

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

Write below code in activity that catching data

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

hope this will help you

Shabbir Dhangot
  • 8,954
  • 10
  • 58
  • 80
0

You should use intent (click here) :

Intent intent = new Intent(getBaseContext(), CustAdrresActivity.class);
intent.putExtra("text", mytext);
startActivity(intent);
Maxouille
  • 2,729
  • 2
  • 19
  • 42
0

You just need to replace your lines:

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

with these three lines:

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

and then, when you open the new activity (in your case the Custaddress Activity), you should do the following to get your result

Bundle extras = getIntent().getExtras();
if (extras != null) {
    String value = extras.getString("result");
}    
Gabriella Angelova
  • 2,985
  • 1
  • 17
  • 30
  • Comes up with an error - "Cannot refer to a non-final local variable 'result' defined in an enclosing scope".. This is on 2nd line of your first lines of code.. – user1641906 May 22 '15 at 13:11
  • just declare your result variable as `final` – Gabriella Angelova May 22 '15 at 13:33
  • 1
    The answer and questions are already there, why do ppl again want to answer such questions, please provide the link or such questions is to be stopped and marked as duplicate, let the asker do some real searching if he needs to learn – DJphy May 22 '15 at 13:39
0

You should add Extra in the Intent object which you are passing in the startActivity(intent) method. Example

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);

In the Custaddress.java Activity class you need to get the data from the Bundle object that you get as a parameter in the onCreate(Bundle bundle) method

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
    }
}
  • Sorry - the second part - where would it fit in my Custaddress code?? @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.cust1_address); final EditText name = (EditText) findViewById(R.id.edittext); final EditText addy = (EditText) findViewById(R.id.edittext1); final EditText cell = (EditText) findViewById(R.id.edittext2); final EditText questions = (EditText) findViewById(R.id.edittext3); Button email = (Button) findViewById(R.id.button1); email.setOnClickListener(new View.OnClickListener() { – user1641906 May 22 '15 at 13:31
  • add it after super.onCreate(savedInstanceState); method call – Samiullah Farooqui May 22 '15 at 13:49
0

Check out this official doc Starting Another Activity.

wwang
  • 605
  • 1
  • 5
  • 13
0

Through the below code we can send the values between activities

use the below code in parent activity

Intent myintent=new Intent(Info.this, GraphDiag.class).putExtra("<StringName>", value);
startActivity(myintent);

use the below code in child activity

String s= getIntent().getStringExtra(<StringName>);
Andriya
  • 241
  • 2
  • 14