I want to send data from three activities to the last activity, how can I do
1. First Activity
else {
//first activity want transfer data last activity
Intent intent = new Intent(Register.this, Register2.class);
intent.putExtra("name", reg_name);
intent.putExtra("surname", reg_surname);
intent.putExtra("email", reg_email);
startActivity(intent);
}
2. Second Activity
else {
//second activity transfer data last activity
Intent intent = new Intent(Register2.this, PhoneNumberOtp.class);
intent.putExtra("username", reg_username);
intent.putExtra("password", reg_password);
startActivity(intent);
}
3. Third Activity
else {
//third activity transfer data last activity
Intent intent = new Intent(PhoneNumberOtp.this, OTPVerification.class);
intent.putExtra("phoneNumber", reg_phone_number);
startActivity(intent);
}
Last Activity
else {
Intent intent = new Intent(OTPVerification.this, HomeActivity.class);
//get data register activity
String name = getIntent().getExtras().getString("name");
String surname = getIntent().getExtras().getString("surname");
String email = getIntent().getExtras().getString("email");
String username = getIntent().getExtras().getString("username");
String password = getIntent().getExtras().getString("password");
CodePudding user response:
Extras you put on an intent are not automatically passed along to subsequent activities after the first one they are sent to directly. If you have some data you want to pass through multiple activities, you have to re-add it to the new sending intent for each step. For example:
First
Intent intent = new Intent(UserActivity.this, AccountActivity.class);
intent.putExtra("name", reg_name);
intent.putExtra("email", reg_email);
startActivity(intent);
Second
Intent intent = new Intent(AccountActivity.this, RegisterActivity.class);
// get the received data and add it to the new intent
Intent recv = getIntent();
if( recv != null ) {
String name = recv.getStringExtra("name");
intent.putExtra("name", name);
String email = recv.getStringExtra("email");
intent.putExtra("email", email);
}
// then add any new data
intent.putExtra("username", reg_username);
intent.putExtra("password", reg_password);
startActivity(intent);
Third
// RegisterActivity now has 4 strings available
Intent recv = getIntent();
if( recv != null ) {
String name = recv.getStringExtra("name");
String email = recv.getStringExtra("email");
String username = recv.getStringExtra("username");
String password = recv.getStringExtra("password");
}
In the example you posted, the last activity would only have access to phoneNumber
, since that's all you passed it from the third activity.