I am trying to pass data from the "addContact" Activity to the "MainActivity"
Here is my vode for "addContact" where I fill a bundle object to be passed through the intent:
public class addContact extends Activity {
private String fname, lname, phoneNumber, email;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addcontact);
}
public void accept(View view){
Intent passdata_intent = new Intent("com.example.contactmanagement.MainActivity");
Bundle bundle = new Bundle();
EditText fname_text = (EditText) findViewById(R.id.fname);
EditText lname_text = (EditText) findViewById(R.id.lname);
EditText phone_text = (EditText) findViewById(R.id.phoneNumber);
EditText email_text = (EditText) findViewById(R.id.email);
fname = fname_text.getText().toString();
lname = lname_text.getText().toString();
phoneNumber = phone_text.getText().toString();
email = email_text.getText().toString();
bundle.putString("fname", fname);
bundle.putString("lname", lname);
bundle.putString("phoneNumber", phoneNumber);
bundle.putString("email",email);
passdata_intent.putExtras(bundle);
startActivity(passdata_intent);
}
protected void cancel(View view){
startActivity(new Intent("com.example.contactmanagement.MainActivity"));
}
}
Here is my code for MainActivity, where I receive the bundle object and attempt to set the text of a TextView
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
Intent intent = getIntent();
Bundle bundle = data.getExtras();
String fname = bundle.getString("fname");
String lname = bundle.getString("lname");
String phoneNumber = bundle.getString("phoneNumber");
String email = bundle.getString("email");
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout layout = (LinearLayout) findViewById(R.id.contactList);
TextView textView1 = (TextView) findViewById(R.id.textView1);
textView1.setText(fname);
/*
TextView textView = new TextView(this);
textView.setLayoutParams(params);
textView.setText(fname);
layout.addView(textView);
*/
}
public void onClick(View view) {
startActivity(new Intent("com.example.contactmanagement.addContact"));
}
}
The text view is not populating with the first name which is "fname".