Android launch new activity
In Android, you can launch a new activity from another activity using the Intent
class. Here's a step-by-step guide:
Step 1: Create a new activity
Create a new Java class that extends the Activity
class. For example, let's say you want to create an activity called SecondActivity
.
Step 2: Define the activity in the AndroidManifest.xml file
Add the new activity to the AndroidManifest.xml
file by declaring it in the <activity>
tag.
<activity
android:name=".SecondActivity"
android:label="@string/title_second_activity" />
Step 3: Create an Intent object
Create an Intent
object that specifies the activity you want to launch.
Intent intent = new Intent(this, SecondActivity.class);
Step 4: Set any necessary data
You can set any necessary data, such as strings, integers, or objects, using the putExtra()
method.
intent.putExtra("key", "value");
Step 5: Start the activity
Use the startActivity()
method to launch the new activity.
startActivity(intent);
Example code
Here's an example code snippet that launches a new activity:
public class FirstActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
// Create an Intent object
Intent intent = new Intent(this, SecondActivity.class);
// Set any necessary data
intent.putExtra("key", "value");
// Start the activity
startActivity(intent);
}
}
Note: Make sure to replace SecondActivity
with the actual name of your new activity.
That's it! By following these steps, you can launch a new activity from another activity in your Android app.