Create a java project that present the Android intents to switch between Activity classes and pass data.
- Create two java files for two activities.
First Activity: Enter your full name and phone number. Then, click “Ok” to go to the Second Activity.
Second Activity: Display Welcoming message with your name.
- Write the java codes for each activity.
- Take screenshot for outputs.
Second Activity (SecondActivity.java)
public class SecondActivity extends AppCompatActivity {
private TextView mWelcomeTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
mWelcomeTextView = findViewById(R.id.welcome_text_view);
Intent intent = getIntent();
if (intent != null) {
String name = intent.getStringExtra(“name”);
String phone = intent.getStringExtra(“phone”);
mWelcomeTextView.setText(“Welcome, ” + name + “! Your phone number is ” + phone + “.”);
}
}
}
First Activity (MainActivity.java)
public class MainActivity extends AppCompatActivity {
private EditText mNameEditText;
private EditText mPhoneEditText;
private Button mOkButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNameEditText = findViewById(R.id.name_edit_text);
mPhoneEditText = findViewById(R.id.phone_edit_text);
mOkButton = findViewById(R.id.ok_button);
mOkButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name = mNameEditText.getText().toString();
String phone = mPhoneEditText.getText().toString();
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra(“name”, name);
intent.putExtra(“phone”, phone);
startActivity(intent);
}
});
}
}