Get text from one activity and set it in another
<p>I seen a lot of questions very similar to mine but nothing seems to be working, so I apologize if this has been answered before.</p>

<p>So what I have is a app with a calculator showing how much money you can save when you roll your own smokes. In the calculator I have a series of edit text boxes for entering: Amount your currently paying for a pack of cigarettes, tax for your state, smokes per day, cost for bag of tobacco, and cost for tubes. Upon hitting calculate it will tell you how much you are currently paying for premade cigarettes vs rolling your own.</p>

<p>The problem that lies is there are many different size bags of tobacco so for everything to work out it has to based off of 6 ounce bags. So what I developed is a worksheet to figure out how much you would pay for the right amount. For instance, if I buy a 16oz bag the calculation wouldn't come out right, so the worksheet will tell you how much you'd pay for a 6oz bag of the same product. </p>

<p>So what I can't figure out is how to take that new price from my worksheet and place it in the calculator activity. From what I read the getText and setText doesn't work between activities. I also seen the intent method where you putExtras from current activity and getExtras on the activity you want it to go to. But every example I've seen involves starting a new activity. When I simply want to take and basically copy the number in worksheet, close the worksheet, and place it in my calculator activity.</p>

<p>I'd greatly appreciate any info you can give me, because I've been searching for this answer for literally months. Ok, I gave up for a while now I'm back on it. If you need me to post code or any other info just let me know.</p>

<p>This is my calculator. This is where I want to put the new number in editText(perbag)</p>

<pre><code>public class Calculator extends Activity {
     EditText perbag

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.calculator);



    final MediaPlayer lighterFlick = MediaPlayer.create(this,
            R.raw.lighter_sound);

    // worksheet clickable textview
    TextView initiateWorksheet = (TextView)           findViewById(R.id.initiateWorksheet);
    initiateWorksheet.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

            startActivity(new Intent("com.trhodes.saveonsmokes.WORKSHEET"));

        }
    });
</code></pre>

<p>This is the worksheet activity which is opened from within the calculator activity. So the first part of this is just getting the number I need in "wsnewprice". So at the bottom I have the button "wsok" which is where I want to take "wsnewprice" of the worksheet, then close out work sheet and place it in the editText "perbag" of the above calculator activity.</p>

<pre><code>    public class Worksheet extends Activity{



EditText wspriceperbag = null;
EditText wsweight = null;
EditText wsnewprice = null;
Button wscalculate;
Button wsok;
double Wspriceperbag = 0.00D;
double Wsweight = 0.00D;
double Wsnewprice = 0.00D;
double ounce = 6;
public Intent intent;







@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.worksheet);

    wspriceperbag = (EditText) findViewById(R.id.wspriceperbag);
    wsweight = (EditText) findViewById(R.id.wsweight);
    wsnewprice = (EditText) findViewById(R.id.wsnewprice);
    wscalculate = (Button) findViewById(R.id.wscalculate);
    wsok = (Button) findViewById(R.id.wsok);







    wscalculate.setOnClickListener(new Button.OnClickListener(){

        public void onClick(View v) {
            // TODO Auto-generated method stub
            try {

                Wspriceperbag = Double.parseDouble(wspriceperbag.getText()
                        .toString());
                Wsweight = Double.parseDouble(wsweight.getText()
                        .toString());
            } catch (NumberFormatException nfe) {

            }
            DecimalFormat localDecimalFormat = new DecimalFormat("#0.00");
            Wsnewprice = (Wspriceperbag / Wsweight) * ounce;
            wsnewprice.setText(localDecimalFormat.format(Wsnewprice));



        }

    });








    wsok.setOnClickListener(new Button.OnClickListener(){

        public void onClick(View v) {
            // TODO Auto-generated method stub






        }

    });
</code></pre>

<p>Now this was the code I was trying to use but like I said I don't want to start a new activity I just want to go to the previous one.</p>

<pre><code>to:

    perbag.setText(getIntent().getExtras().getString("newprice"));


from:

    Intent intent = new Intent(Worksheet.this, Calculator.class);
            intent.putExtra("newprice",wsnewprice.getText().toString());
            startActivity(intent);
</code></pre>

<p>I'm sorry if this is confusing, I'm trying to be as clear as possible.</p>

<p>Edit: for the solution I found</p>

<p>new code for original activity</p>

<pre><code> //this is to start the worksheet
        initiateWorksheet.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
Intent i = new Intent(Calculator.this, Worksheet.class);
startActivityForResult(i, 0);


//this is coming back to the original activity and placing the new data..
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK);
        Bundle bundle = data.getExtras();
        String s = bundle.getString("newprice");
        perbag.setText(s);
</code></pre>

<p>new code for the worksheet activity:</p>

<pre><code>//this is getting the new number and putting it into a string
wsok.setOnClickListener(new Button.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent newNumber = new Intent();
            Bundle bundle = new Bundle();
            bundle.putString("newprice", wsnewprice.getText().toString());
            newNumber.putExtras(bundle);
            setResult(RESULT_OK, newNumber);
            finish();


//had to add this because force close when hitting the back button
    @Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    Intent newNumber = new Intent();
    Bundle bundle = new Bundle();
    bundle.putString("newprice", "");
    newNumber.putExtras(bundle);
    setResult(RESULT_OK, newNumber);
    finish();
</code></pre>

<p>Thanks to everyone you responded, without you guys I'd still be pulling my hair out.
Plus I'm all ears if anyone has a suggestion on the force closing on hitting the back button with out the last lines of code. Otherwise is cool cause it's actually working the way I want.</p>

<hr>

<p>thanks,
Travis</p>
