In this program I have three activities Main, Second and Third. When the app opens there is a blank textview and button. Selecting that button will bring you to the second activity where the user can enter a name, phone number, and email address. Once those forms are filled out the user will hit a button to store that information and bring them back to the main activity. When brought back to the main activity the user will see that the name they entered is displayed in the once blank textview. They may then select that textview which will bring them to the third activity displaying the name, phone number and email address they entered in the second activity.
I have everything working expect passing the data from the second activity to the third. I have done some research and I see a lot can be done with bundles and shared prefs. I am not too familiar with them, as this is very new to me. I did try implementing them, but have not had any luck. Anyways the code is below and any help, feedback or guidance would be highly appreciated. Thank you in advance!
Main Activity:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View view){
startActivityForResult(new Intent(getApplicationContext(),SecondActivity.class),999);
}
public void onClickText(View v)
{
startActivityForResult(new Intent(getApplicationContext(),ThirdActivity.class),999);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 999 && resultCode == RESULT_OK)
{ TextView contactView = (TextView) findViewById(R.id.contactDisplay);
contactView.setText(data.getStringExtra("Name"));
}
}}
Second Activity:
public class SecondActivity extends AppCompatActivity {
public String textName;
public String emailAddress;
public int phoneNumber;
private TextView textNameView2;
EditText textPersonName;
EditText number;
EditText textEmailAddress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
public void onClickTwo(View view)
{
textPersonName = (EditText) findViewById(R.id.name);
number = (EditText) findViewById(R.id.number);
textEmailAddress = (EditText) findViewById(R.id.email);
textName = textPersonName.getText().toString();
emailAddress = textEmailAddress.getText().toString();
phoneNumber = Integer.valueOf(number.getText().toString());
showToast(textName +"added");
Intent backMain = new Intent(this, MainActivity.class);
// Intent backMain = new Intent();
backMain.putExtra("Name",textName);
// backMain.putExtra("Email", emailAddress);
// backMain.putExtra("Phone", phoneNumber);
setResult(RESULT_OK, backMain);
Intent thirdMain = new Intent(this, ThirdActivity.class);
thirdMain.putExtra("Name",textName);
thirdMain.putExtra("Email",emailAddress);
thirdMain.putExtra("Phone",phoneNumber);
setResult(RESULT_OK,thirdMain);
finish();
}
private void showToast(String text)
{
Toast.makeText(this,text, Toast.LENGTH_LONG).show();
}}
Third Activity:
public class ThirdActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 999 && resultCode == RESULT_OK) {
TextView contact = (TextView) findViewById(R.id.contactNameView);
contact.setText(data.getStringExtra("Name"));
TextView phone = (TextView) findViewById(R.id.phoneView);
phone.setText(data.getStringExtra("Phone"));
TextView email = (TextView) findViewById(R.id.emailView);
email.setText(data.getStringExtra("Email"));
}
}
}