In an Android application, how do you start a new activity (GUI) when a button in another activity is clicked, and how do you pass data between these two activities?
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.
onClick
attribute of the Button. (Beginner)OnClickListener()
via an anonymous class. (Intermediate) switch
statement. (Pro)Here's the link to my example if you want to follow along:
onClick
attribute of the Button. (Beginner)Buttons have an onClick
attribute that is found within the .xml file:
In Java class:
@Override
protected 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 onClick
s to the same intent.
Disadvantage: Difficult readability when reviewing.
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:
@Override
protected 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.
switch
statement. (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:
@Override
protected 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);
}
@Override
public 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?