1

Hi i'v got two activities going to one activity. When the activity is opened there is a number displayed from either of the two activities

I need to know where the number came from so I can do the appropriate calculations, I think i need an if statement but don't know how to start it

Intent intent = new Intent(calculateA.this,
    WAmounts.class);
    intent.putExtra("Result",Total);
    startActivity(intent);


Intent intent = new Intent(CalcB.this,
    WAmounts.class);
    intent.putExtra("ResultB",Total);
        startActivity(intent);

Third activiity

 Intent sender = getIntent();
    int result = sender.getExtras().getInt("Result");
    answer.setText(result+"");


    int resultB = sender.getExtras().getInt("ResultB");
    answer.setText(resultB+"");

//If number came from first activity  

    a = Integer.parseInt (answer.getText().toString());
    total = (float) (a *x); 
    sd.setText(String.format("%.1f" ,total));



    b = Float.parseFloat (sand.getText().toString());
    total1 = (int)Math.ceil (b*f);
    c.setText(Integer.toString(total1));

  //If number came from second activity     

  a = Integer.parseInt (answer.getText().toString());
  total = (float) (a *x); 
  sd.setText(String.format("%.1f" ,total));



  b = Float.parseFloat (sand.getText().toString());
  total1 = (int)Math.ceil (b*f);
  c.setText(Integer.toString(total1));
Robert
  • 1,107
  • 2
  • 8
  • 8

2 Answers2

3

Just pass it some "extras" like shown here: How do I pass data between Activities in Android application?

intent.putExtra("yourLabel", "text1");

then in the new activity use

if (extras != null) {
    String value = extras.getString("yourLabel");
}
Community
  • 1
  • 1
HalR
  • 11,411
  • 5
  • 48
  • 80
  • 1
    This is what I do. I create a `"key"` to send called '"source"` and assign a tag that I can easily remember then check what `"source"` is when I get my `Intent` in the receiving `Activity` – codeMagic Apr 09 '13 at 20:38
1

since you call your extras "result" and "resultB" you could use sender.hasExtra("result"), and if it returns false - you know which activity started this intent.

Note that bot sender and sender1 in your code get the same intent - the one that started this activity. You don't have to get it twice.

scf
  • 396
  • 2
  • 19