In Android Studio choose New Project → Empty Views Activity, with Language Java and Minimum SDK API 24.
2. Add a second screen
Right-click your package → New → Activity → Empty Views Activity, named SecondActivity. Android Studio registers it in the manifest.
3. Open it with an Intent
An Intent says "open this screen". new Intent(this, SecondActivity.class), then start it.
4. Send data along
intent.putExtra("name", ...) attaches the name. In SecondActivity, getIntent().getStringExtra("name") reads it back.
5. Go back
finish() closes the second screen and returns to the first, just like the Back button.
6. Send a reply back
Start the screen with registerForActivityResult. In SecondActivity, call setResult(RESULT_OK, intent) before finish(). The first screen receives the reply in its callback.
7. Run it
Type your name, open the second screen, then tap Wave back.
Tip
Use extras for small values like names and IDs. For bigger data, save it (for example in a database) and pass just the ID.
What's improved over the video
It passes data between screens and back again, using the current registerForActivityResult API.
Files affected
Check what to add and what to replace before copying code.
Opens SecondActivity with the name, and receives its reply.
MainActivity.java
packagecom.example.switchactivities;importandroid.content.Intent;importandroid.os.Bundle;importandroid.widget.Button;importandroid.widget.TextView;importandroidx.activity.result.ActivityResultLauncher;importandroidx.activity.result.contract.ActivityResultContracts;importandroidx.appcompat.app.AppCompatActivity;importcom.google.android.material.textfield.TextInputEditText;/**
* Opens SecondActivity with an Intent, passing the name along, and gets a reply back when it closes.
*/publicclassMainActivityextendsAppCompatActivity{TextView reply;// Starts SecondActivity and receives its reply when the user comes back.privatefinalActivityResultLauncher<Intent> openSecond =registerForActivityResult(newActivityResultContracts.StartActivityForResult(), result ->{if(result.getResultCode()==RESULT_OK&& result.getData()!=null){
reply.setText(getString(R.string.reply, result.getData().getStringExtra(SecondActivity.EXTRA_REPLY)));}});@OverrideprotectedvoidonCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);Screens.setUp(this,R.layout.activity_main);TextInputEditText name =findViewById(R.id.name);
reply =findViewById(R.id.textReply);Buttonopen=findViewById(R.id.btnOpen);open.setOnClickListener(v ->{Intent intent =newIntent(this,SecondActivity.class);// Extras carry small pieces of data to the next screen.
intent.putExtra(SecondActivity.EXTRA_NAME,String.valueOf(name.getText()).trim());
openSecond.launch(intent);});}}