0

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".

hippoman
  • 187
  • 2
  • 10
  • What does it populate textView1 with? What does fname contain when it's received, if you look at the variable in the debugger? – fejd Mar 11 '19 at 20:41
  • it doesn't populate textview at all. in the debugger i am getting an error "no activity found to handle Intent { act=com.example.contactmanagement.MainActivity (has extras) } and it is cause by "at com.example.contactmanagement.addContact.accept(addContact.java:41)" if I debug the app and put a break point in the onActivityResult part, it never reaches the breakpoint, if i put it in the addContact accept function, it fills the bundle up correctly – hippoman Mar 11 '19 at 20:47

1 Answers1

0

Move the Intent handling in MainActivity to onCreate and make sure the activities are declared in AndroidManifest.xml. Use the classes directly instead of hard coded strings when you create the intents. The addContact class should also use a capital first letter. Check the Google documentation for more information.

fejd
  • 2,545
  • 1
  • 16
  • 39
  • my app won't start if i put it in the onCreate area. My app isn't running my onActivityResult() code. Do you have any other suggestions? – hippoman Mar 11 '19 at 22:28
  • It sounds like you want to use startActivityForResult in MainActivity and then fill in the data in addContact and call setResult and finish there. See https://stackoverflow.com/a/10407371/383676 for an example. – fejd Mar 11 '19 at 22:52