Keywords: Android | Activity | Intent | Bundle | Calling
Abstract: This article delves into methods for calling one Activity from another in Android, covering Intent usage, Manifest configuration, data passing, and dialog Activity calls. With detailed code examples and step-by-step explanations, it helps developers master inter-Activity communication to enhance app development.
Fundamentals of Android Activity Calling
In Android development, an Activity is a fundamental UI component that manages screen interactions. Calling one Activity from another is a common requirement, typically involving the Intent mechanism. Intent is a core class in Android for inter-component communication, encapsulating actions and data to enable Activity transitions and data exchange.
To call another Activity, first declare the target Activity in the AndroidManifest.xml file. For example, if the target Activity is named ListViewImage, add the following code:
<activity android:name="ListViewImage"></activity>This ensures the system recognizes and can launch the Activity. Then, in the current Activity, create an Intent object and call startActivity to initiate the new Activity. A typical example involves implementing this in a button click event:
ImageButton btListe = (ImageButton) findViewById(R.id.Button_Liste);
btListe.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(CurrentActivity.this, ListViewImage.class);
startActivity(intent);
finish(); // optional, to finish the current Activity
}
});This code demonstrates jumping from CurrentActivity to ListViewImage, where the finish() method ends the current Activity to free resources.
Passing Data with Intent and Bundle
In real-world apps, passing data between Activities is often necessary. Android supports this through Bundle and Intent extras. Bundle is a key-value pair container for storing primitive data types or objects, transmitted via Intent to the target Activity.
For instance, to pass a long value from Activity1 to Activity2, implement as follows:
// In Activity1
EditText editValue = (EditText) findViewById(R.id.edit_value);
Button sendButton = (Button) findViewById(R.id.send_button);
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String valueString = editValue.getText().toString();
long value = 0;
try {
value = Long.parseLong(valueString);
} catch (NumberFormatException e) {
value = 0; // default value
}
Bundle sendBundle = new Bundle();
sendBundle.putLong("value", value);
Intent i = new Intent(Activity1.this, Activity2.class);
i.putExtras(sendBundle);
startActivity(i);
finish();
}
});In Activity2, receive and process the data:
// In Activity2
Bundle receiveBundle = getIntent().getExtras();
if (receiveBundle != null) {
long receiveValue = receiveBundle.getLong("value", 0); // default 0
EditText receiveValueEdit = (EditText) findViewById(R.id.receive_edit);
receiveValueEdit.setText(String.valueOf(receiveValue));
}
Button callReceiverButton = (Button) findViewById(R.id.call_button);
callReceiverButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Activity2.this, Receiver.class);
i.putExtra("new_value", receiveValue - 10);
startActivity(i);
}
});This approach enables flexible data flow between Activities, enhancing app interactivity.
Calling Dialog Activities
Sometimes, it's necessary to call an Activity that displays as a dialog, e.g., for prompts or inputs. In Android, this can be achieved by setting the Activity's theme to a dialog style. The calling method is similar to regular Activities, but attention to UI design is required.
Declare a dialog Activity in the Manifest:
<activity android:name="DialogActivity" android:theme="@android:style/Theme.Dialog"></activity>When calling from the current Activity, the Intent creation and startup process remains the same. For example:
Intent dialogIntent = new Intent(CurrentActivity.this, DialogActivity.class);
startActivity(dialogIntent);This ensures the dialog Activity appears modally, requiring user response before returning.
In summary, Activity calling in Android relies on the Intent mechanism, combined with Manifest declarations and data passing, to enable efficient UI transitions and functional extensions. Developers should master these core concepts to build seamless user experiences.