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?
Starting an activity from another activity is very common scenario among android applications.
To start an activity you need an Intent object.
An intent object takes two parameter in its constructor
Example:
So for example,if you have two activities, say HomeActivity
and DetailActivity
and you want to start DetailActivity
from HomeActivity
(HomeActivity-->DetailActivity).
Here is the code snippet which shows how to start DetailActivity from
HomeActivity.
Intent i = new Intent(HomeActivity.this,DetailActivity.class);
startActivity(i);
And you are done.
Coming back to button click part.
Button button = (Button) findViewById(R.id.someid);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(HomeActivity.this,DetailActivity.class);
startActivity(i);
}
});