I. startup
1: Start startActivity directly)
◆ Declare the Intent object to specify the startup source and startup target, and then let the Intent object start as a parameter of the startActivity method.
◆ Implementation:
Intent I = new Intent (MainActivity. this, Act2.class); // start the source and start the target startActivity (I); // directly start the Method |
2: Start startActivity with the carrying value)
◆ Declare the Intent object to specify the startup source and startup target, call the putExtra method of the Intent object, carry data in the form of key-value pairs, and then enable the Intent object as a parameter of the startActivity method.
◆ Implementation:
1) Main Activity
Intent I = new Intent (start source. this, start target. class); I. putExtra ("key name", "value"); startActivity (I ); |
2) Target Activity
String val = getIntent (). getStringExtra ("key name"); // get the passed Value |
3: Start startActivityForResult with return values)
In some scenarios, we need to jump from interface A to carry data to interface B. After some processing on interface B, the corresponding operations have been completed after returning the returned values to interface, this is the startup with the returned value.
◆ The specific process is as follows:
1) Create an Intent object in the main Activity and specify the startup source and target.
2) The main Activity calls the putExtra method of the Intent object and carries data in the form of key-value pairs.
3) The main Activity calls the startActivityForResult method. There are two parameters. The first is the Intent object, the second is requestCode, and the second is the int type. To determine which interface triggers the action
4) Create an empty Intent object for the target Activity, call the putExtra method, and store data as key-value pairs.
5) prepare data for the target Activity. At this time, only data is prepared and has not been transferred. The startup is triggered only after the finish interface. SetResult method, two parameters, the first resultCode, indicates the result returned by that interface, and the second parameter Intent object
6) Target Activity: Terminate the target Activity. After the finish method is executed, start the returned value.
7) rewrite the onActivityResult method of the main Activity and perform different operations by comparing the parameter values.
◆ Specific implementation
1) The main Activity starts with the return value through the button
Public class MainActivity extends Activity {private Button button; // declaration Button @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); button = (Button) findViewById (R. id. button1); // find the button by id. setOnClickListener (new OnClickListener () {// Add listener @ Override public void onClick (View arg0) {Intent I = new Intent (MainActivity. this, Act2.class); // defines the Intent object, indicating that the source object is startActivityForResult (I, 1); // the startup method with the returned value }});} @ Override protected void onActivityResult (int requestCode, int resultCode, Intent data) {switch (requestCode) {// identify the action returned by that interface case 1: switch (resultCode) {// determine which return value case 1: System. out. println (data. getStringExtra ("act2"); break; default: System. out. println ("resultCode Value Mismatch"); break;} break; default: System. out. println ("requestCode Value Mismatch"); break;} super. onActivityResult (requestCode, resultCode, data );}} |
2) Target Activity
Public class Act2 extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. act2); Intent I = new Intent (); // empty Intent object I. putExtra ("act2", "I am act2"); // Save the data setResult (1, I); // prepare the data finish (); // terminate the Activity }} |
◆ Result: the returned value is output on the console.
08-21 13:34:50. 777: I/System. out (451): act2 |
Ii. Disable
1: close itself to finish)
The finish method is called when the Activity completes all tasks and needs to be closed. Once this method is executed, the Activity will execute the onDestory method in the life cycle. Therefore, we can put the resource recycling operation in onDestory, call the finish method, and indirectly call the onDestory method, the Activity has been closed.
2: Disable other activityfinishactivities)
FinishActivity is used to determine which Activity to close based on the value of requestCode. There can be multiple activities that can be closed by this method, but these activities must be started by a method with a returned value.
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1106326245-0.gif "/> my brain capacity ....
This article is from the "Schindler" blog, please be sure to keep this source http://cinderella7.blog.51cto.com/7607653/1280199