Android page Jump and data exchange, android jump

Source: Internet
Author: User

Android page Jump and data exchange, android jump

Author: Qing Dujun

Address: http://blog.csdn.net/qingdujun/article/details/40015757


This article demonstrates the entire process of redirection and data exchange on the Android interface through a small Demo.

The effect is as follows:

1) MainActivity. java

Package doogle. xian. bundletest; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. view. menu; import android. view. menuItem; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. editText; public class MainActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); Button btn = (Button) findViewById (R. id. btn); btn. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View arg0) {EditText name = (EditText) findViewById (R. id. name); EditText age = (EditText) findViewById (R. id. age); CData send = new CData (name. getText (). toString (), Integer. parseInt (age. getText (). toString (); // create a Bundle object Bundle data = new Bundle (); data. putSerializable ("mydata", send); // create an Intent object Intent intent = new Intent (MainActivity. this, ResultActivity. class); intent. putExtras (data); // start ActivitystartActivity (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 ;}@ Override public boolean onOptionsItemSelected (MenuItem item) {// Handle action bar item clicks here. the action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest. xml. int id = item. getItemId (); if (id = R. id. action_settings) {return true;} return super. onOptionsItemSelected (item );}}

2) activity_main.xml

<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: paddingBottom = "@ dimen/activity_vertical_margin" android: paddingLeft = "@ dimen/plugin" android: paddingRight = "@ dimen/plugin" android: paddingTop = "@ dimen/plugin" tools: context = "doogle. xian. bundletest. mainActivity "> <TextView android: id =" @ + id/TV "android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: text =" passed to Activity2: "/> <EditText android: id =" @ + id/name "android: layout_width =" fill_parent "android: layout_height =" wrap_content "android: layout_below = "@ id/TV" android: hint = "name"/> <EditText android: id = "@ + id/age" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: layout_below = "@ id/name" android: hint = "age"/> <Button android: id = "@ + id/btn" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_below = "@ id/age" android: text = "Activate activity2"/> </RelativeLayout>

3) ResultActivity. java

Package doogle. xian. bundletest; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. widget. textView; public class ResultActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. result); TextView tv1 = (TextView) findViewById (R. id. tv1); TextView tv2 = (TextView) findViewById (R. id. tv2); // get the Intent intent = getIntent () that starts the Result; // retrieve the data CData accept = (CData) Intent in the Bundle packet carried by the intent directly. getSerializableExtra ("mydata"); tv1.setText ("name" + accept. getName (); tv2.setText ("Age" + accept. getAge ());}}
4) result. xml

<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical"> <TextView android: id = "@ + id/TV" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "received data:"/> <TextView android: id = "@ + id/tv1" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = ""/> <TextView android: id = "@ + id/tv2" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = ""/> </LinearLayout>
5) CData. java

package doogle.xian.bundletest;import java.io.Serializable;public class CData implements Serializable{private static final long serialVersionUID = 1L;String name;int age;public CData(String n, int a) {name = n;age = a;}public String getName(){return name;}public int getAge(){return age;}}
6) AndroidMainfest. xml

<? Xml version = "1.0" encoding = "UTF-8"?> <Manifest xmlns: android = "http://schemas.android.com/apk/res/android" package = "doogle. xian. bundletest "android: versionCode =" 1 "android: versionName =" 1.0 "> <uses-sdk android: minSdkVersion =" 15 "android: targetSdkVersion = "15"/> <application android: allowBackup = "true" android: icon = "@ drawable/ic_launcher" android: label = "@ string/app_name" android: theme = "@ style/AppTheme"> <activity android: name = ". mainActi Category "android: label =" @ string/app_name "> <intent-filter> <action android: name =" android. intent. action. MAIN "/> <category android: name =" android. intent. category. LAUNCHER "/> </intent-filter> </activity> <! -- Configure Activity --> <activity android: name = ". ResultActivity" android: label = "Activity2"> </activity> </application> </manifest>

Address: http://blog.csdn.net/qingdujun/article/details/40015757



In Android, how do I transfer the data in interface A to interface B but jump to interface C? Use Intent?

I don't understand. Why do you want to jump to the C interface if you pass something on the interface to B. However, values transmitted between different activities are indeed implemented using Intent. The putExtra () method of intent can store values in intent. For more information, see the API

StartActivity directly opens the Activity to which the intent points. In this way, you can pass the information to the C page, and then pass the value to B when you click the C tab. At the same time, the B page is opened.

How to achieve redirection on the android Interface

Intent intent = new Intent ();
Intent. setClass (**. this, **. activity );
StartActivity (intent );

(**. This, **. activity) The first is the current activity, and the second is the activity to jump.

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.