-2

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

}
}
JoeMan
  • 3
  • 2

1 Answers1

0

Follow this thread on passing data between activities using intent:

How do I pass data between Activities in Android application?

However, if you want to pass data using shared preference, Use this code snippet:

public class SharedPreferenceManager {
    private static final String PREFS_NAME = Config.SHARED_PREF_NAME;

    /**
     * @param context
     * @param key
     * @param value
     * @return
     */
    public static boolean saveToPreference(Context context, String key, String value) {
        SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString(key, value);
        return editor.commit();
    }


    /**
     * @param context
     * @param key
     * @return
     */
    public static String loadFromPreference(Context context, String key) {
        SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        return settings.getString(key, "");
    }
}

To save to preference you pass in the application context, The key and the String you want to save. To load the saved string from SharedPreference you pass in the Context and the Key in the activity lets say i wan to save a string email to my shared preference in one activity and load it in another, this is how i would do it:

//Save to shared Preference in Activity A
SharedPreferenceManager.saveToPreference(this, "email", emailAddress.getText().toString());

//Load from shared preference in activity b
String emailAddress = SharedPreferenceManager.loadFromPreference(this, "email");
Asendo
  • 158
  • 1
  • 10