Intent, intent
1. Use show Intent
Intent intent = new Intent (FirstActivity. this, SecondActivity. class );
StartActivity (intent );
The above code is used to open the SecondActivity.
2. Use implicit Intent
First open AndroidManifest. xml and add the Code:
<Activity
Android: name = "com. example. activitytest. SecondActivity"
Android: label = "@ string/title_activity_second">
<Intent-filter>
<Action android: name = "com. example. activitytest. ACTION_START"/>
<Category android: name = "android. intent. category. DEFAULT"/>
<Category android: name = "com. example. activitytest. MY_CATEGORY"/>
</Intent-filter>
</Activity>
Then modify the Click Event of the button in FirstActivity:
Btn. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
Intent intent = new Intent ("com. example. activitytest. ACTION_START ");
Intent. addCategory ("com. example. activitytest. MY_CATEGORY ");
StartActivity (intent );
}
});
You can also use implicit Intent to start activities of other programs.
Intent intent = new Intent (Intent. ACTION_VIEW );
Intent. setData (Uri. parse ("http://www.cnblogs.com/zhouhb "));
// Intent intent = new Intent (Intent. ACTION_DIAL );
// Intent. setData (Uri. parse ("tel: 12345 "));
3. Use Intent to transmit data between activities
3.1 transmit data from FirstActivity to SecondActivity
String s = "from first ";
Intent intent = new Intent (FirstActivity. this, SecondActivity. class );
Intent. putExtra ("data", s );
StartActivityForResult (intent, 1 );
3.2 SecondActivity receives transmitted data
Button btn = (Button) findViewById (R. id. button2 );
Intent intent = getIntent ();
String string = intent. getStringExtra ("data ");
Btn. setText (string );
3.3 SecondActivity returns data to FirstActivity
Btn. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View arg0 ){
// TODO Auto-generated method stub
ReturnData ();
}
});
Private void returnData (){
Intent intent = new Intent ();
Intent. putExtra ("returnData", "from Second ");
SetResult (RESULT_ OK, intent );
Finish ();
}
// If you press Back to return to FirstActivity instead of clicking the button, override onBackPressed
Public void onBackPressed (){
// TODO Auto-generated method stub
ReturnData ();
}
3.4 rewrite onActivityResult in FirstActivity to get the data returned by SecondActivity
Protected void onActivityResult (int requestCode, int resultCode, Intent data ){
// TODO Auto-generated method stub
Switch (requestCode ){
Case 1:
If (resultCode = RESULT_ OK ){
String string = data. getStringExtra ("returnData ");
Toast. makeText (this, string, Toast. LENGTH_SHORT). show ();
}
Break;
Default:
Break;
}
}