Here is the code for the main activity:
Development: Passing value between activities -
package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
(savedInstanceState);
setContentView();
Button button =(Button)findViewById();
(new () {
//Register the click event for the button and open a new activity
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Set the component to be activated for the Intent (the Activity of TheOtherActivity will be activated)
Intent intent =new Intent(,);//
//Writing method 1 (, );//Set the component to be activated
//Writing method two (new ComponentName(, ));//Set the component to be activated
//The first way to pass values (the code looks more concise)
/*
("name", "dinglang");
("age", 22);
*/
//The second way of transferring value
Bundle bundle =new Bundle();
("name", "dinglang");
("age", 22);
(bundle);
/*
Intent provides various putExtra() methods after common types overloading, such as: putExtra(String name, String value), putExtra(String name, long value). In the putExtra() method, it will determine whether there is a Bundle object inside the current Intent object. If it does not exist, a new Bundle object will be created. The value passed in the future by calling the putExtra() method will be stored in the Bundle object.
These can actually be viewed through the source code, and the internal implementation principles are the same
*/
//startActivity(intent);//You can directly activate it without receiving the return value of the component.
//Return result needs to be received. Pay attention to the returned result code
startActivityForResult(intent, 100);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
(this, ("result"), 1).show();//Get the return result
(requestCode, resultCode, data);
}
}
Here is the otherActivity part of the code:
Under the same package, create a new class, inherit to the Activity class, and override the onCreate method...
package ;
import ;
import ;
import ;
import ;
import ;
import ;
public class TheOtherActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
(savedInstanceState);
setContentView();//Set the xml layout file corresponding to this activity
Intent intent =();//Get her intention to activate
String name =("name");
int age=().getInt("age");//The second way of value selection
TextView textView = (TextView)();
("Name: "+ name+" Age: "+ age);
Button button = (Button)();
(new () {
//Return the result to the previous Activity
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent =new Intent();
("result", "This is the result of processing");
setResult(20, intent);//Set return data
finish();//Close activity
}
});
}
}
When creating new activities, be careful to create a new XML layout file in the layout folder. (If you choose to create an Activity when creating a new Android project, a new XML layout file will be created by default)
Here is the layout file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open OtherActivity"
android:
/>
</LinearLayout>
Below is the layout file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:andro
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is OtherActivity"
android:
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Close Activity"
android:
/>
</LinearLayout>
Finally, pay attention to modifying the project manifest file. Add in it and register a new Activity name
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:andro
package=""
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="" />
<category android:name="" />
</intent-filter>
</activity>
<!-- Note that --> should be added to the project list file
<activity android:name="TheOtherActivity" android:label="the other Activity"/>
</application>
</manifest>
Knowledge points to note:
When using Intent component attachment data, two ways to pass values between activities.
It is worth mentioning that the role of the Bundle class
The Bundle class is used as carrying data, which is similar to Map, and is used to store values in the form of key-value name-value pairs. Compared with Map, it provides various commonly used types of putXxx()/getXxx() methods, such as: putString()/getString() and putInt()/getInt(). putXxx() is used to put data into the Bundle object, and getXxx() method is used to obtain data from the Bundle object. The inside of the Bundle actually uses a variable of type HashMap<String, Object> to store the values placed in the putXxx() method.
Also, in the onActivityResult method, the first parameter is the request code, that is, call startActivityForResult() to pass the past value, and the second parameter is the result code, which is used to identify which new Activity the returned data comes from. They all play a simple role of identification (don't be confused with the status codes such as 404, 200 in the http protocol). You can fill in and match according to your business needs. If necessary, you can judge based on this.
I won't give an in-depth explanation here.