Android: Basic Data Type for Activity data transmission

Source: Internet
Author: User

Android: Basic Data Type for Activity data transmission

Since there must be two data transfer activities between activities, we must first create two activities, in MainActivity

Add a button component and click the button to jump to another Activity to transfer data.
The jump between activities can be achieved through explicit display, like this

Intent intent=new Intent();intent.setClass(MainActivity.this, OtherActivity.class);startActivity(intent);

But how can we pass data over? We can pass it through the putExtra () method. Here we pass a string type data
Intent. putExtra ("name", "I am Su ");

This is similar to the map set, where "name" is the key, "I am Su" is the value, one-to-one relationship. Use putExtra () to store string in intent

In OtherActivity, getIntent (). getExtra () is used to obtain the data in the Intent object. getExtra () returns the Bundle object.

Received through the Bundle object,Then print it in the log file.

 

Bundle bundle = getIntent (). getExtras (); String name = bundle. getString ("name"); Toast. makeText (OtherActivity. this, name, Toast. LENGTH_LONG ). show ();
The running result is as follows. We can see that we pass data from one Activity to another.

Of course, other basic data types, such as Double int, can also be passed as long as they are changed when receiving the data, such as receiving the double type

Double x=bundle.getDouble(key);

Code Attached

MainActivity

Package com. example. activity; import android. OS. bundle; import android. app. activity; import android. content. intent; import android. view. menu; import android. view. view; import android. view. view. onClickListener; import android. widget. button; public class MainActivity extends Activity {private Button button; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); button = (Button) findViewById (R. id. button1); button. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View arg0) {// TODO Auto-generated method stubIntent intent = new Intent (); intent. setClass (MainActivity. this, OtherActivity. class); intent. putExtra ("name", "I am Su"); startActivity (intent );}});}}

OtherActivity
Package com. example. activity; import android. app. activity; import android. OS. bundle; import android. util. log; import android. widget. toast; public class OtherActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stubsuper. onCreate (savedInstanceState); setContentView (R. layout. activity_other); Bundle bundle = getIntent (). getExtras (); String name = bundle. getString ("name"); Toast. makeText (OtherActivity. this, name, Toast. LENGTH_LONG ). show ();}}

Activity_main.xml
 
  
 

Activity_other.xml
<!--?xml version="1.0" encoding="utf-8"?--><linearlayout android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"></linearlayout>
ActivityManifest
<!--?xml version="1.0" encoding="utf-8"?--><manifest android:versioncode="1" android:versionname="1.0" package="com.example.activity" xmlns:android="http://schemas.android.com/apk/res/android">    <uses-sdk android:minsdkversion="8" android:targetsdkversion="18">    <application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme">        <activity android:label="@string/app_name" android:name="com.example.activity.MainActivity">            <intent-filter>                <action android:name="android.intent.action.MAIN">                <category android:name="android.intent.category.LAUNCHER">            </category></action></intent-filter>        </activity>        <activity android:name="com.example.activity.OtherActivity"></activity>    </application></uses-sdk></manifest>

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.