1

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);
            }


        }


    });
}

}

  • Unclear what is this asked there. – Roman C Dec 30 '17 at 00:07
  • This would be much simpler if you had one activity rather than three. That being said... in `phoneNumber.putExtra("phoneMessage", phoneField.getText().toString())`, you are attaching an extra named `phoneMessage` to the `Intent`. The value is a `String`. in `intent.getBundleExtra("phoneMessage")`, you are trying to retrieve a `Bundle` named `phoneMessage` as an extra. However, there is no `Bundle` named `phoneMessage`. As [Kehinde points out](https://stackoverflow.com/a/48029750/115145), you need to use `getStringExtra()`, not `getBundleExtra()`. – CommonsWare Dec 30 '17 at 00:19
  • Yes, I would agree that one activity that scrolled would have been easier, but since I wanted the users to input information to the form one screen at a time I made multiple activities. I am sure there is a better way. I have only been coding for 1 month though. – Joshua Edgar Dec 30 '17 at 01:02

2 Answers2

1

In your AgreementActivity, get the phone number using:

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

  • I used this code instead of bundle but the String still comes up as Null once I get to the final activity and launch the email – Joshua Edgar Dec 30 '17 at 00:53
  • @JoshuaEdgar But AgreementActivity is the final activity right? – Emmanuel Kehinde Dec 30 '17 at 00:57
  • Yes it is. I have 8 activities all together. I am only showing the Main Activity (phone number) and then the Agreement Activity (where it submits the info gained from all the others) – Joshua Edgar Dec 30 '17 at 01:00
  • @JoshuaEdgar Since AgreementActivity is being launched from the MainActivity, then phoneMessage shouldn't be null. – Emmanuel Kehinde Dec 30 '17 at 01:02
  • Is it possible that the input text from EditText field is getting destroyed before the user get to the final activity because it is not carried over to all the activities in between? – Joshua Edgar Dec 30 '17 at 01:07
  • Agreement Activity is not launched from Main Activity. I have that intent going to another activity "Warning". – Joshua Edgar Dec 30 '17 at 01:09
  • @JoshuaEdgar First and foremost, you shouldn't try starting two activities at the same time (in the same block of code) – Emmanuel Kehinde Dec 30 '17 at 01:11
  • @JoshuaEdgar If you really want to persist the data till the last activity when it's needed, there are a number of ways you can get it done: You can simply pass it between activities. You can save it in static variable in a separate class. You can save it using SharedPreference and call it when needed. – Emmanuel Kehinde Dec 30 '17 at 01:14
  • Thank you for helping. So when I pass it between the activities, what am I doing wrong? Ive looked up passing objects from activity to activity but cannot find anything showing how to pass it through multiple activities ie. 8 – Joshua Edgar Dec 30 '17 at 01:25
  • @JoshuaEdgar You pass it between activities using extras (just as you have done it earlier) – Emmanuel Kehinde Dec 30 '17 at 01:27
  • So I added the intents all the way from the mainActivity step by step all the way to the Agreement Submit button. Seems to be working. However, I get this error that says one of my activities needs to be exported or have an intent filter? It was running just fine, but then this just started happening? – Joshua Edgar Dec 30 '17 at 06:43
  • @JoshuaEdgar You can check out these solutions https://stackoverflow.com/a/41156434 or https://stackoverflow.com/a/11551693 – Emmanuel Kehinde Dec 30 '17 at 07:54
0

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

Pomagranite
  • 696
  • 5
  • 11