Android Program Development: (2) use Intent-2.4 use Intent to transmit data

Source: Internet
Author: User

In addition to the ability to return data results from an Activity, it is also very common to transmit data to an Activity.
1. Create a project named PassData.
 
2. Code in main. xml.
 
[Java] view plaincopy <? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">
 
<Button
Android: id = "@ + id/btn_SecondActivity"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: onClick = "onClick"
Android: text = "Click to go to Second Activity"/>
 
</LinearLayout>
3. In the res/layout folder, create the secondactivity. xml file. [Java] view plaincopy <? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">
 
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "Welcome to Second Activity"/>
 
<Button
Android: id = "@ + id/btn_MainActivity"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: onClick = "onClick"
Android: text = "Click to return to main activity"/>
 
</LinearLayout>
4. Create an Activity subclass: SecondActivity. java. [Java] view plaincopypublic class SecondActivity extends Activity {
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. secondactivity );
 
// --- Get the data passed in using getStringExtra ()---
Toast. makeText (this, getIntent (). getStringExtra ("str1 "),
Toast. LENGTH_SHORT). show ();
 
// --- Get the data passed in using getIntExtra ()---
Toast. makeText (this,
Integer. toString (getIntent (). getIntExtra ("age1", 0 )),
Toast. LENGTH_SHORT). show ();
 
// --- Get the Bundle object passed in ---
Bundle bundle = getIntent (). getExtras ();
 
// --- Get the data using the getString ()---
Toast. makeText (this, bundle. getString ("str2"), Toast. LENGTH_SHORT)
. Show ();
 
// --- Get the data using the getInt () method ---
Toast. makeText (this, Integer. toString (bundle. getInt ("age2 ")),
Toast. LENGTH_SHORT). show ();
}
 
Public void onClick (View view ){
// --- Use an Intent object to return data ---
Intent I = new Intent ();
 
// --- Use the putExtra () method to return some
// Value ---
I. putExtra ("age3", 45 );
 
// --- Use the setData () method to return some value ---
I. setData (Uri. parse ("Something passed back to main activity "));

// --- Set the result with OK and the Intent object ---
SetResult (RESULT_ OK, I );
 
// --- Destroy the current activity ---
Finish ();
}
}
5. The code in AndroidManifest. xml. [Java] view plaincopy <? Xml version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: android = "http://schemas.android.com/apk/res/android"
Package = "net. horsttnann. PassingData"
Android: versionCode = "1"
Android: versionName = "1.0" type = "codeph" text = "/codeph">
 
<Uses-sdk android: minSdkVersion = "10"/>
 
<Application
Android: icon = "@ drawable/ic_launcher"
Android: label = "@ string/app_name">
<Activity
Android: name = ". PassingDataActivity"
Android: label = "@ string/app_name">
<Intent-filter>
<Action android: name = "android. intent. action. MAIN"/>
 
<Category android: name = "android. intent. category. LAUNCHER"/>
</Intent-filter>
</Activity>
<Activity
Android: name = "net. horsttnann. PassingData. SecondActivity"
Android: label = "Second Activity">
<Intent-filter>
<Action android: name = "net. horsttnann. PassingDataSecondActivity"/>
 
<Category android: name = "android. intent. category. DEFAULT"/>
</Intent-filter>
</Activity>
</Application>
 
</Manifest>
 
6. The code in PassDataActivity.
 
 
[Java] view plaincopyprint? Public class PassingDataActivity extends Activity {
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
}

Public void onClick (View view ){
Intent I = new
Intent ("net. learn2develop. PassingDataSecondActivity ");
 
// --- Use putExtra () to add new key/value pairs ---
I. putExtra ("str1", "This is a string ");
I. putExtra ("age1", 25 );
 
// --- Use a Bundle object to add new key/values
// Pairs ---
Bundle extras = new Bundle ();
Extras. putString ("str2", "This is another string ");
Extras. putInt ("age2", 35 );
 
// --- Attach the Bundle object to the Intent object ---
I. putExtras (extras );
 
// --- Start the activity to get a result back ---
StartActivityForResult (I, 1 );
}

Public void onActivityResult (int requestCode,
Int resultCode, Intent data)
{
// --- Check if the request code is 1 ---
If (requestCode = 1 ){
 
// --- If the result is OK ---
If (resultCode = RESULT_ OK ){
 
// --- Get the result using getIntExtra ()---
Toast. makeText (this, Integer. toString (
Data. getIntExtra ("age3", 0 )),
Toast. LENGTH_SHORT). show ();
 
// --- Get the result using getData ()---
Toast. makeText (this, data. getData (). toString (),
Toast. LENGTH_SHORT). show ();
}
}
}
 
}
Public class PassingDataActivity extends Activity {
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
}

Public void onClick (View view ){
Intent I = new
Intent ("net. learn2develop. PassingDataSecondActivity ");
 
// --- Use putExtra () to add new key/value pairs ---
I. putExtra ("str1", "This is a string ");
I. putExtra ("age1", 25 );
 
// --- Use a Bundle object to add new key/values
// Pairs ---
Bundle extras = new Bundle ();
Extras. putString ("str2", "This is another string ");
Extras. putInt ("age2", 35 );
 
// --- Attach the Bundle object to the Intent object ---
I. putExtras (extras );
 
// --- Start the activity to get a result back ---
StartActivityForResult (I, 1 );
}

Public void onActivityResult (int requestCode,
Int resultCode, Intent data)
{
// --- Check if the request code is 1 ---
If (requestCode = 1 ){
 
// --- If the result is OK ---
If (resultCode = RESULT_ OK ){
 
// --- Get the result using getIntExtra ()---
Toast. makeText (this, Integer. toString (
Data. getIntExtra ("age3", 0 )),
Toast. LENGTH_SHORT). show ();
 
// --- Get the result using getData ()---
Toast. makeText (this, data. getData (). toString (),
Toast. LENGTH_SHORT). show ();
}
}
}
 
}
7. debug by F11.
 
 
 
 
:
 
The program is started for the first time:
 



Jump to SecondActivity:

Return PassDataActivity:


Written by manoel

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.