I want to start a new activity when I click a button on Android, what should I do?

Asked 1 years ago, Updated 1 years ago, 102 views

How do I display a new activity when I click the activity button on the Android app?

Also, will it be possible to deliver data between these two activities then?

android android-intent android-activity android-button android-lifecycle

2022-09-22 15:20

1 Answers

From the onClick method of the button

Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);
myIntent.putExtra("key", value); //Insert data to transfer CurrentActivity.this.startActivity(myIntent); //Start LiveActivity

Inserting these codes will allow you to run new activities.

@Override
protected void onCreate(Bundle savedInstanceState) {
Intent intent = getIntent();
String value = int.getStringExtra("key"); //Data to be forwarded

Then, the onCreate method of the newly launched activity receives the data like the code above.

Oh, don't forget to register for a new activity in the Android manifest file. <activity android:label="@string/app_name" android:name="NextActivity"/>


2022-09-22 15:20

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.