3

I have created 3 activities: firstActivity, secondActivity, and thirdActivity.

  1. firstActivity is the main activity.
  2. secondActivity will work only if it receives an SMS, and it then sends the message to thirdActivity.
  3. thirdActivity converts the string values to double values, and then sends the double values to firstActivity.

I can send values from secondActivity to thirdActivity, but I don't know how to pass values ​​from thirdActivity to firstActivity. Please advise me on how I should do this.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Zephyr
  • 45
  • 6

3 Answers3

1

There are several approaches to this.

One would be using a custom application class to store sort of "global values" for your entire application. That way, you would set the values in firstActivity and use them in thirdActivity.

Take a look at this SO question to learn how to store global state in Android applications.

Community
  • 1
  • 1
Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292
1

Whenever you have an activity called for a result, like in this case you have thirdActivity you can always use the method startActivityForResult instead of just startActivity. Once thirdActivity has finished its processing and it wants to return the result value, it should call setResult and that's it.

The problem with this approach here is that you kind of have secondActivity in the middle. Is it really necessary?

Bilthon
  • 2,461
  • 8
  • 36
  • 44
  • Yes, it is necessary. I want to receive sms using BroadcastReceiver and send it to thirdActivity. From your answer, could you give me an example? – Zephyr Jan 20 '12 at 04:36
  • I would say that you don't really need 3 activities for what you are doing there. The parsing procedure that you now do in thirdActivity could very well be done in the firstActivity whenever it gets the "raw" message from secondActivity (which I presume is a BroadcastReceiver). – Bilthon Jan 24 '12 at 02:18
0

Thank you for your help. Now I can pass values from thirdActivity to firstActivity, this is how I do.

ThirdActivity :

public class ThirdActivity extends Activity {

double value1, value2;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ...

    value1 = Double.parseDouble(value1FromSecondActivity);
    value2 = Double.parseDouble(value2FromSecondActivity);      

    Intent intent = new Intent(this, FirstActivity.class);
    intent.putExtra("Value1", value1);
    intent.putExtra("Value2", value2);
    startActivity(intent);
    finish();
    }
} 

FirstActivity :

public class FirstActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ...
}   

@Override
protected void onResume() {
    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        dValue1 = extras.getDouble("Value1");
        dValue2 = extras.getDouble("Value2");
        Toast.makeText(getBaseContext(), dValue1 + " : " + dValue2, Toast.LENGTH_SHORT).show();
    }
    else {
        Toast.makeText(getBaseContext(), "Null", Toast.LENGTH_SHORT).show();
    }
    super.onResume();
    }
}
Zephyr
  • 45
  • 6