Navigating between screens and exchanging data between them is a core requirement in Android application development. This process relies heavily on the Intent class, which acts as a messaging object to request an action from another component. The navigation stack in Android functions similarly to a container where Activities are pushed onto the stack when started and popped off when finished.
1. Basic Activity Navigation
To transition from one Activity to another, an explicit Intent is created. This Intent specifies the current context and the target Activity class. The startActivity() method is then invoked to execute the transition.
Below is an example of triggering a navigation event inside a button click listener:
Button navigateButton = findViewById(R.id.btn_navigate);
navigateButton.setOnClickListener(v -> {
// Create an explicit Intent to launch the DetailActivity
Intent navigationIntent = new Intent(MainActivity.this, DetailActivity.class);
// Initiate the transition
startActivity(navigationIntent);
});
2. Forwarding Data to the Target Activity
Data can be attached to an Intent before it is launched using key-value pairs. The putExtra() method facilitates this. In the following snippet, a string value is packaged into the Intent:
String messageContent = "Data transmitted from the source screen";
navigationIntent.putExtra("EXTRA_MESSAGE", messageContent);
startActivity(navigationIntent);
Inside the receiving Activity (DetailActivity), the getIntent() method retrieves the Intent that started the Activity. The stored data can then be extracted using the corresponding key and type-specific getter methods:
// Retrieve the Intent
Intent sourceIntent = getIntent();
String receivedMessage = sourceIntent.getStringExtra("EXTRA_MESSAGE");
// Display the data
TextView displayText = findViewById(R.id.tv_display);
displayText.setText(receivedMessage);
// Setup a button to close the current Activity
Button closeBtn = findViewById(R.id.btn_close);
closeBtn.setOnClickListener(v -> finish());
3. Launching System Features via Imlpicit Intents
Intents can also be used to invoke system-level applications like the web browser or the dialer. These are known as Implicit Intents, where the action is specified rather than a specific component.
Opening a Web Page:
Using Intent.ACTION_VIEW with a URI directs the system to open the URL in a browser.
Button webButton = findViewById(R.id.btn_open_web);
webButton.setOnClickListener(v -> {
Uri webpage = Uri.parse("https://www.example.com");
Intent webIntent = new Intent(Intent.ACTION_VIEW, webpage);
startActivity(webIntent);
});
Opening the Dialer:
Using Intent.ACTION_DIAL prepares the phone application to dial a specific number.
Button dialButton = findViewById(R.id.btn_dial);
dialButton.setOnClickListener(v -> {
Uri phoneNumber = Uri.parse("tel:10086");
Intent dialIntent = new Intent(Intent.ACTION_DIAL, phoneNumber);
startActivity(dialIntent);
});
4. Receiving Data Back from a Child Activity
For scenarios where the parent Activity requires a result from the child Activity it launches, the startActivityForResult() method is used. When the child Activity finishes, it returns data via an Intent, which is captured in the onActivityResult() callback of the parent.
Launching for Result:
The second parameter in startActivityForResult is a request code used to identify the callback.
Button resultButton = findViewById(R.id.btn_get_result);
resultButton.setOnClickListener(v -> {
Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
startActivityForResult(intent, REQUEST_CODE_SETTINGS);
});
Sending the Result Back:
In the child Activity (SettingsActivity), a result code and an Intent containing data are passed to setResult() before calling finish().
Button returnButton = findViewById(R.id.btn_return_data);
returnButton.setOnClickListener(v -> {
Intent returnIntent = new Intent();
returnIntent.putExtra("RETURN_DATA", "Settings updated successfully");
// Set the result and close the Activity
setResult(RESULT_OK, returnIntent);
finish();
});
Handling the Result:
The parent Activity overrides onActivityResult() to process the returned data. The request code verifies the source of the result.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_SETTINGS && resultCode == RESULT_OK) {
String result = data.getStringExtra("RETURN_DATA");
TextView statusView = findViewById(R.id.tv_status);
statusView.setText(result);
}
}