Android transfers data from one Activity to another: androidactivity

Source: Internet
Author: User

Android transfers data from one Activity to another: androidactivity

MainActivity: click a Button to jump to another NextActivity.

Package com. example; import java. util. arrayList; 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 {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setCont EntView (R. layout. activity_main); Button button = (Button) findViewById (R. id. but1); button. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {// TODO Auto-generated method stub Intent intent = new Intent (MainActivity. this, NextActivity. class);/** second type * Intent intent2 = new Intent (); * intent2.setClass (MainActivity. this, NextActivity. class); * Third: * Intent intent3 = ne W Intent ("com. example. nextActivity "); * Note: This method must be used to configure the action attribute in the intent-filter tag in the Manifest configuration file. ** It is equivalent to: * Intent intent3 = new Intent (" com. example. nextActivity "); * intent3.setAction (com. example. nextActivity "); ** do not forget the startActivity (intent) method! */Intent. putExtra ("name", "zhangsan"); intent. putExtra ("age", 20); ArrayList <String> arrayList = new ArrayList <String> (); arrayList. add ("1"); arrayList. add ("2"); arrayList. add ("4"); intent. putStringArrayListExtra ("unknow", arrayList); startActivity (intent) ;}}@override public boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}}

AndroidManifest file:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="17" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.example.MainActivity"            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=".NextActivity">            <intent-filter >                <action android: name="com.example.NextActivity"/>                <category android:name="android.intent.category.DEFAULT" />            </intent-filter>                    </activity>    </application></manifest>

Obtain data from MainActivity in NextActivity:

Package com. example;

Import java. util. ArrayList;

Import android. app. Activity;
Import android. content. Intent;
Import android. OS. Bundle;
Import android. util. Log;

Public class NextActivity extends Activity {
Private final String TAG = "NextActivity ";

@ Override
Protected void onCreate (Bundle savedInstanceState ){
// TODO Auto-generated method stub
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_next );
Intent intent = getIntent ();
String name = intent. getStringExtra ("name ");
Log. I (TAG, "-- name -->" + name );
Int age = intent. getIntExtra ("age", 0 );
Log. I (TAG, "-- age -->" + age );
ArrayList <String> arrayList = intent. getStringArrayListExtra ("unknow ");
For (String s: arrayList ){
Log. I (TAG, s );

}

}

}

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.