Joshua Edgar
Joshua Edgar

Reputation: 13

Using Multiple intents with different activities and final email intent

I am making a Form App that collects information and sends it in an email. MainActivity collects phone #, then to a warning message activity, then to Name Activity etc. The issue that I am having is sending the data collected from the EditText fields to the final AgreementActivity to be sent as an email is not working for me. I have looked everywhere but I cannot figure out how to send the user to the next activity while sending the input data to the final Agreement activity so it can be sent in an email.

This is what I have so far.

 public class MainActivity extends AppCompatActivity {

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



    // Find the next button
    final Button next1 = (Button) findViewById(R.id.button);

    // Find the Edit Text Field to input Phone Number
    final EditText phoneField = (EditText) findViewById(R.id.edit_text_phone);

    // Set a click listener on the next button
    next1.setOnClickListener(new View.OnClickListener() {
        // The code in this method will be executed when the next1 Button is clicked on.
        @Override
        public void onClick(View view) {

            // sends data collected from edit text box to be sent to final page so it can be
            // sent in an email message.

            Intent phoneNumber = new Intent(MainActivity.this, AgreementActivity.class);
            phoneNumber.putExtra("phoneMessage", phoneField.getText().toString());
            startActivity(phoneNumber);

            //starts next activity
            Intent nextButton = new Intent(MainActivity.this, Warning.class);
            startActivity(nextButton);
        }
    });

}

}

This is the final Agreement Activity where it will send an email. But the final result in the email subject line is "your phone number is null"

public class AgreementActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.agreement_activity);


    // Find the submit button
    final Button submit = (Button) findViewById(R.id.button6);


    // This is the input from user for phone number field
    final Bundle phoneNumber = getIntent().getExtras();



    // Set a click listener on that View
    submit.setOnClickListener(new View.OnClickListener() {
        // The code in this method will be executed when the Submit Button is clicked on.
        @Override
        public void onClick(View view) {



            Intent intent = new Intent(Intent.ACTION_SENDTO);
            intent.setData(Uri.parse("mailto:"));
            intent.getBundleExtra("phoneMessage");
            intent.putExtra(Intent.EXTRA_SUBJECT, "Your Phone Number is " + phoneNumber);

            if (intent.resolveActivity(getPackageManager()) != null) {
                startActivity(intent);
            }


        }


    });
}

}

Upvotes: 1

Views: 378

Answers (2)

Pomagranite
Pomagranite

Reputation: 696

I would extend application class like this however you could find other ways. You do need to consider the backstack if someone navigates back arrow

Upvotes: 0

Emmanuel Kehinde
Emmanuel Kehinde

Reputation: 41

In your AgreementActivity, get the phone number using:

final String phoneNumber = getIntent().getStringExtra("phoneMessage");

Upvotes: 1

Related Questions