Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start new activity on button click

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?

like image 439
Adham Avatar asked Nov 15 '10 15:11

Adham


People also ask

How do I intent to another activity?

The following code demonstrates how you can start another activity via an intent. # Start the activity connect to the # specified class Intent i = new Intent(this, ActivityTwo. class); startActivity(i); Activities which are started by other Android activities are called sub-activities.


1 Answers

Easy.

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

Extras are retrieved on the other side via:

@Override protected 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"/> 
like image 121
Emmanuel Avatar answered Oct 18 '22 00:10

Emmanuel