0

I'm writing an android app that tracks your weight and BMI. In order to update or set the two values for the first time, there is a second activity that allows me to input the values that should then pass to the first activity.

I've tried to achieve this in a few ways, the closest I got is the code below:

Second Activity: (UpdateProgressActivity.java)

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_body_progress);

    Intent intentWeight = getIntent();
    String WeightText = intentWeight.getStringExtra(EXTRA_MESSAGE);
    TextView messageWeight = findViewById(R.id.weight);
    messageWeight.setText(WeightText);

    Intent intentBMI = getIntent();
    String BMIText = intentBMI.getStringExtra(EXTRA_MESSAGE);
    TextView messageBMI = findViewById(R.id.bmi);
    messageBMI.setText(BMIText);


    updateButton = findViewById(R.id.updateBodyProgress);
    updateButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            openActivityUpdateProgress();
        }
    });

}

First Activity: (BodyProgressActivity.java)

public class UpdateProgressActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_update_progress);


    Button confirmProgress;
    confirmProgress = findViewById(R.id.confirmBodyStatus);
    confirmProgress.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            returnToActivityProgress();
        }
    });

}

protected void returnToActivityProgress() {
    Intent intent = new Intent(this, BodyProgressActivity.class);
    startActivity(intent);
}

public void createStatus(View view) {
    EditText messageWeight = findViewById(R.id.textNewWeight);
    String textWeight = messageWeight.getText().toString();
    Intent intentWeight = new Intent(Intent.ACTION_SEND); intentWeight.setType("text/plain");
    intentWeight.putExtra(Intent.EXTRA_TEXT, textWeight);
    String textWeigthTemp = getString(R.string.currentWeight);
    Intent chosenIntentWeight = Intent.createChooser(intentWeight, textWeigthTemp);
    startActivity(chosenIntentWeight);

    EditText messageBMI = findViewById(R.id.textNewWeight);
    String textBMI = messageBMI.getText().toString();
    Intent intentBMI = new Intent(Intent.ACTION_SEND); intentBMI.setType("text/plain");
    intentWeight.putExtra(Intent.EXTRA_TEXT, textBMI);
    String textBMITemp = getString(R.string.currentBMI);
    Intent chosenIntentBMI = Intent.createChooser(intentWeight, textBMITemp);
    startActivity(chosenIntentBMI);
}

}

I'm getting the values from "textNewWeight" and "textNewBMI" from the second activity and must pass them to "weight" and "bmi" on the first activity. What I'm getting in the first activity is just blank.

GeorgeS
  • 97
  • 6

1 Answers1

2

In the first activity. Use this code. We are using putExtra method to send the data to next activity through key and value. Key is the 1st parameter and value is the 2nd parameter. Key name can be your choice and it is used to retrieve the data in the 2nd activity. Value is the data which you want ti send to next activity.

    Intent i=new Intent(firstactivity.this,secondactivity.class);
    String weight=messageWeight.getText().toString();
    i.putExtra(“weight”,weight);
    startActivity(i);

In the second activity,use this to recieve the data.

   Bundle b=getIntent().getExtras();
   String recievedWeight=b.getString(“weight”);

the parameter name in the b.getString(“”) must be same as which you have declared in the 1st activity.

Thrishool MSM
  • 297
  • 1
  • 11