Objective:
Today in the software "The Art of War" to optimize the military simulation of the selection of equipment functions, learning how to use Startactivityforresult.
Because the original selection interface is a list of the form of display, only equipment name, no corresponding picture, and more than 200 props back and forth to choose too much trouble, so think of the GridView control to display the selection interface.
If you select a prop in a new activity and pass the item's ID number back to the original activity, the original activity state is guaranteed to be the same.
Normal startactivity to jump two activity is not possible, because the original activity was created again.
Baidu a bit, found Startactivityforresult this is perfect to meet the requirements of my effect.
Body:
The main function of Startactivityforresult is that it can return data, assuming we have two pages, first into the first page, there is a button to enter the next page, when entering the next page, set the operation, and in its finish () After the action or back action, the set value is passed to the first page so that the first page displays the resulting value. This is a bit like a callback method, that is, after the second page of the finish () action or back action, the Onactivityresult () method of the first page is recalled.
Here's a demo, a total of two activity
Let's take a look at the code:
1 Package and other ;2 3 import COM.EXAMPLE.ALLCODE.R;4 5 import android.app.Activity;6 import android.content.Intent;7 import Android.os.Bundle;8 import Android.view.View;9 import Android.view.View.OnClickListener;Ten import Android.widget.Button; One import Android.widget.TextView; A - Public classUse_startactivityforresult_frist extends Activity implements onclicklistener{ - PrivateTextView text_frist; the PrivateTextView Text_second; - PrivateButton btn_frist; - PrivateButton Btn_second; - intRequestcode =0; + Intent Intent; - @Override + protected voidonCreate (Bundle savedinstancestate) { A //TODO auto-generated Method Stub at super.oncreate (savedinstancestate); - Setcontentview (r.layout.other_frist_activity); -Text_frist =(TextView) Findviewbyid (r.id.id_frist_text); -Text_second =(TextView) Findviewbyid (r.id.id_second_text); -Btn_frist =(Button) Findviewbyid (r.id.btn_frist); -Btn_second =(Button) Findviewbyid (r.id.btn_second); inBtn_frist.setonclicklistener ( This); -Btn_second.setonclicklistener ( This); to + -Intent =NewIntent (); theIntent.setclass (use_startactivityforresult_frist. This, Use_startactivityforresult_second.class); * $ }Panax Notoginseng @Override - Public voidOnClick (View v) { the //TODO auto-generated Method Stub + Switch(V.getid ()) { A Caser.id.btn_frist: the //the value of the request code is arbitrarily set, but must be >=0 +Requestcode =0; - Startactivityforresult (Intent, requestcode); $ Break; $ CaseR.id.btn_second: -Requestcode =2; - Startactivityforresult (Intent, requestcode); the Break; - default:Wuyi Break; the } - Wu } - About $ //callback method that executes this method when it returns from the second page - @Override - protected voidOnactivityresult (intRequestcode,intResultCode, Intent data) { -String change01 = Data.getstringextra ("change01");//get the data for CHANGE01 AString change02 = Data.getstringextra ("CHANGE02");//get the data for CHANGE02 + //according to the previous request sent above to distinguish the Switch(requestcode) { - Case 0: $Text_frist.settext (CHANGE01);//perform the appropriate action according to the request code the Break; the Case 2: the Text_second.settext (CHANGE02); the Break; - default: in Break; the } the About } the the the}First Activity
1 Package and other ;2 3 import COM.EXAMPLE.ALLCODE.R;4 5 import android.app.Activity;6 import android.content.Intent;7 import Android.os.Bundle;8 9 Public classUse_startactivityforresult_second extends activity{Ten intResultCode =0; One @Override A protected voidonCreate (Bundle savedinstancestate) { - //TODO auto-generated Method Stub - super.oncreate (savedinstancestate); the Setcontentview (r.layout.other_second_activity); - -Intent Intent =NewIntent (); -Intent.putextra ("change01","The first row value has changed and is now:");//give the CHANGE01 parameter a value of +Intent.putextra ("CHANGE02","The second row value has changed and is now:");//give the CHANGE02 parameter a value of 2000 note this value, that is, the second argument must be of type string - //set the results and transfer them + This. Setresult (ResultCode, intent); A This. Finish (); at } - -}a second activity
1<?xml version="1.0"encoding="Utf-8"?>2<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"3android:orientation="Vertical"4Android:layout_width="fill_parent"5android:layout_height="fill_parent"6>7<TextView8Android:id="@+id/id_frist_text"9Android:layout_width="fill_parent" Tenandroid:layout_height="wrap_content" Oneandroid:text="first line of text information" A/> -<TextView -Android:id="@+id/id_second_text" theAndroid:layout_width="fill_parent" -android:layout_height="wrap_content" -android:text="second line of text information" -/> +<Button -Android:id="@+id/btn_frist" +Android:layout_width="fill_parent" Aandroid:layout_height="wrap_content" atandroid:text="change the value of the first line of text" -/> -<Button -Android:id="@+id/btn_second" -Android:layout_width="fill_parent" -android:layout_height="wrap_content" inandroid:text="change the value of the second line of text" -/> to</LinearLayout>
the first activity corresponding layout file
:
function of the request code
Using the Startactivityforresult (Intent Intent, int requestcode) method to open a new activity, we need to pass in a request code for the Startactivityforresult () method ( Second parameter). The value of the request code is self-set according to the business need and is used to identify the source of the request. For example: An activity with two buttons, click on both buttons will open the same activity, whether it is the button to open the new activity, when the new activity is closed, the system will call the previous activity Onactivityresult (int requestcode, int resultcode, Intent data) method (this method needs to be overridden).
Effect of the result code
In an activity, you might use the Startactivityforresult () method to open several different activities to handle different businesses, and when these new activity is closed, The system invokes the previous activity's onactivityresult (int requestcode, int resultcode, Intent data) method. In order to know which new activity the returned data comes from, you can do so in the Onactivityresult () method (Resultactivity and newactivity are the new activity to open):
Detailed usage of Android development _startactivityforresult