How to start new activity on button click How to start new activity on button click android android

How to start new activity on button click


Easy.

Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);myIntent.putExtra("key", value); //Optional parametersCurrentActivity.this.startActivity(myIntent);

Extras are retrieved on the other side via:

@Overrideprotected void onCreate(Bundle savedInstanceState) {    Intent intent = getIntent();    String value = intent.getStringExtra("key"); //if it's a string you stored.}

Don't forget to add your new activity in the AndroidManifest.xml:

<activity android:label="@string/app_name" android:name="NextActivity"/>


Current responses are great but a more comprehensive answer is needed for beginners. There are 3 different ways to start a new activity in Android, and they all use the Intent class; Intent | Android Developers.

  1. Using the onClick attribute of the Button. (Beginner)
  2. Assigning an OnClickListener() via an anonymous class. (Intermediate)
  3. Activity wide interface method using the switch statement. (not-"Pro")

Here's the link to my example if you want to follow along:

  1. Using the onClick attribute of the Button. (Beginner)

Buttons have an onClick attribute that is found within the .xml file:

<Button    android:id="@+id/button1"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:onClick="goToAnActivity"    android:text="to an activity" /><Button    android:id="@+id/button2"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:onClick="goToAnotherActivity"    android:text="to another activity" />

In Java class:

@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.main_activity);}public void goToAnActivity(View view) {    Intent intent = new Intent(this, AnActivity.class);    startActivity(intent);}public void goToAnotherActivity(View view) {    Intent intent = new Intent(this, AnotherActivity.class);    startActivity(intent);}

Advantage: Easy to make on the fly, modular, and can easily set multiple onClicks to the same intent.

Disadvantage: Difficult readability when reviewing.

  1. Assigning an OnClickListener() via an anonymous class. (Intermediate)

This is when you set a separate setOnClickListener() to each button and override each onClick() with its own intent.

In Java class:

@Overrideprotected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main_activity);        Button button1 = (Button) findViewById(R.id.button1);        button1.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                Intent intent = new Intent(view.getContext(), AnActivity.class);                view.getContext().startActivity(intent);}            });        Button button2 = (Button) findViewById(R.id.button2);        button2.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                Intent intent = new Intent(view.getContext(), AnotherActivity.class);                view.getContext().startActivity(intent);}            });

Advantage: Easy to make on the fly.

Disadvantage: There will be a lot of anonymous classes which will make readability difficult when reviewing.

  1. Activity wide interface method using the switch statement. (not-"Pro")

This is when you use a switch statement for your buttons within the onClick() method to manage all the Activity's buttons.

In Java class:

@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.main_activity);    Button button1 = (Button) findViewById(R.id.button1);    Button button2 = (Button) findViewById(R.id.button2);    button1.setOnClickListener(this);    button2.setOnClickListener(this);}@Overridepublic void onClick(View view) {    switch (view.getId()){        case R.id.button1:            Intent intent1 = new Intent(this, AnActivity.class);            startActivity(intent1);            break;        case R.id.button2:            Intent intent2 = new Intent(this, AnotherActivity.class);            startActivity(intent2);            break;        default:            break;    }

Advantage: Easy button management because all button intents are registered in a single onClick() method


For the second part of the question, passing data, please see How do I pass data between Activities in Android application?

Edit: not-"Pro"


Create an intent to a ViewPerson activity and pass the PersonID (for a database lookup, for example).

Intent i = new Intent(getBaseContext(), ViewPerson.class);                      i.putExtra("PersonID", personID);startActivity(i);

Then in ViewPerson Activity, you can get the bundle of extra data, make sure it isn't null (in case if you sometimes don't pass data), then get the data.

Bundle extras = getIntent().getExtras();if(extras !=null){     personID = extras.getString("PersonID");}

Now if you need to share data between two Activities, you can also have a Global Singleton.

public class YourApplication extends Application {          public SomeDataClass data = new SomeDataClass();}

Then call it in any activity by:

YourApplication appState = ((YourApplication)this.getApplication());appState.data.CallSomeFunctionHere(); // Do whatever you need to with data here.  Could be setter/getter or some other type of logic