In an Activity, you can use the startActivity (Intent intent) method provided by the system to open a new Activity. before opening a new Activity, you can decide whether to pass parameters for the new Activity.
First, open a new Activity without passing parameters.
Public class MainActivity extends Activity {
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Button btnOpen = (Button) this. findViewById (R. id. btnOpen );
BtnOpen. setOnClickListener (new View. OnClickListener (){
Public void onClick (View v ){
// Create an explicit intent. The first parameter is the current Activity class object, and the second parameter is the Activity class to be opened for you.
StartActivity (new Intent (MainActivity. this, OtherActivity. class ));
}
});
}
}
Type 2: open a new Activity and pass several parameters to it
Package com. ljq. activitys;
Import android. app. Activity;
Import android. content. Intent;
Import android. OS. Bundle;
Import android. view. View;
Import android. widget. Button;
Public class MainActivity extends Activity {
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Button btnOpen = (Button) this. findViewById (R. id. btnOpen );
BtnOpen. setOnClickListener (new View. OnClickListener (){
Public void onClick (View v ){
// Type 1: open a new Activity without passing Parameters
// Create an explicit intent. The first parameter is the current Activity class object, and the second parameter is the Activity class to be opened for you.
// StartActivity (new Intent (MainActivity. this, OtherActivity. class ));
// Type 2: open a new Activity and pass several parameters to it
Intent intent = new Intent (MainActivity. this, OtherActivity. class );
// Bundle class used to carry data
Bundle bundle = new Bundle ();
Bundle. putString ("name", "linjiqin ");
Bundle. putInt ("age", 24 );
// Additional data attached
Intent. putExtras (bundle );
StartActivity (intent );
}
});
}
}
Receive parameters passed by the previous Activity in the new Activity.
Package com. ljq. activitys;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. util. Log;
Public class OtherActivity extends Activity {
Private final static String TAG = "OtherActivity ";
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. other );
// Receive parameters passed by the previous Activity in the new Activity
Bundle bundle = this. getIntent (). getExtras ();
String name = bundle. getString ("name ");
Integer age = bundle. getInt ("age ");
Log. I (TAG, name + ":" + age );
}
}