Transfer of information between Android activity

Source: Internet
Author: User

In Android, activity is an interface that requires intent (intent) for message delivery when it is necessary to switch between interfaces. Use the startactivity (Intent) function to start another activity.

Bundle is a Key-value class that can carry information with him.

The bundle.putstring ("Key", "String") method and the Bundle.getstring ("key") method are put in and out of the information.

The information can be put into intent, using the method Intent.putextras (bundle) method and the Intent.getextras () method to put information into and out.


Example project: two. Java,mainactivity,java Enter a string, click the button, start Newactivity,java, and display the string.

The detailed code is as follows:

Mainactivity.java:

Import android.app.activity;import android.app.actionbar;import android.app.fragment;import  android.content.intent;import android.os.bundle;import android.view.layoutinflater;import  android.view.Menu;import android.view.MenuItem;import android.view.View;import  Android.view.view.onclicklistener;import android.view.viewgroup;import android.widget.button;import  android.widget.EditText;import android.widget.TextView;import android.os.Build;public  class mainactivity extends activity {    edittext edittext;     TextView textview;     @Override     protected  void oncreate (bundle savedinstancestate)  {         super.oncreate (savedinstancestate);         setcontentview ( R.layout.activity_main);          button botton1= (Button) This.findviewbyid (R.ID.BTN1);         edittext= (EditText) This.findviewbyid (r.id.edit);         textview= (TextView) This.findviewbyid (R.ID.MYTEXT1);         botton1.setonclicklistener (New onclicklistener ()  {              @Override              public void onclick (view arg0)  {                 // TODO Auto-generated method stub                 intent intent= New intent (Mainactivity.this,newactivity.class);                &nBsp Bundle bundle=new bundle ();                 bundle.putstring ("Name",  edittext.gettext (). toString ());                 intent.putextras (bundle);                 startactivity (Intent);             }         });     }}

Newactivity.java:

import android.app.activity;import android.content.intent;import android.os.bundle;import  android.view.view;import android.view.view.onclicklistener;import android.widget.button;import  android.widget.textview;public class newactivity extends activity {     textview textview;    protected void oncreate (Bundle  savedinstancestate)  {        super.oncreate (savedInstanceState);         setcontentview (r.layout.activity_new);         button botton1= (Button) This.findviewbyid (R.ID.BTN2);         textview= (TextView) This.findviewbyid (R.ID.MYTEXT3);         intent intent=getintent ();        bundle  Bundle=intent.getextras ();  &Nbsp;      textview.settext (bundle.getstring ("name"));         botton1.setonclicklistener (New onclicklistener ()  {              @Override              public void onclick (view arg0)  {                 // todo auto-generated method  stub                 StartActivity (New intent (newactivity.this,mainactivity.class));             }        });     }}

Activity_main:

<?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:id= "@+id/mytext1"          android:layout_width= "Fill_parent"         android:layout_ height= "Wrap_content"         android:text= "mainactivity"/>     <edittext android:id= "@+id/edit"          Android:layout_width= "match_parent"         android:layout_height= "wrap _content "        android:text=" "/>    < Button android:id= "@+id/btn1"           android:layout_width= "Wrap_content"          android:layout_height= "Wrap_content"         android: text= "Tonew"/>    </linearlayout>

Activity_new:

<?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/mytext2"          android:layout_width= "Fill_parent"         android:layout_ height= "Wrap_content"         android:text= "newactivity"/>     <textview android:id= "@+id/mytext3"          Android:layout_width= "Fill_parent"         android:layout_height= "Wrap_ Content "        android:text=" "/>    <button  android:id= "@+id/btn2" &NBSP;&NBsp;      android:layout_width= "Wrap_content"          android:layout_height= "Wrap_content"         android: text= "Tomain"/></linearlayout>

The effect is as follows:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/59/D2/wKioL1TrAa3zXt3mAAClN7HnFvQ365.jpg "title=" 1.png " alt= "wkiol1traa3zxt3maacln7hnfvq365.jpg"/> 650) this.width=650; "Src=" http://s3.51cto.com/wyfs02/M00/59/D2/ Wkiol1trauajz7feaacvr5y_qiq186.jpg "style=" Float:none; "title=" 2.png "alt=" wkiol1trauajz7feaacvr5y_qiq186.jpg "/ >


start activity and return results

Sometimes our college starts an activity and returns its results, which is how we should use it:

Startactivityforresult (Intent intent,int requestcode);

by overriding Onactivityresult () to process the returned parameters, the function prototype is:

protected void Onactivityresult (int requestcode,int resultcode,intent data)//request code and result code

Use function NewActivity.this.setResult (0,intent) and NewActivity.this.finish () to set return parameters


Add code based on one of the above works. Causes the key in the Newactivity.java to return a string, and then displays the string in Mainactivity.

The code is as follows:

Mainactivity

public class mainactivity extends activity {    edittext  edittext;    textview textview;     @Override      protected void oncreate (bundle savedinstancestate)  {         super.oncreate (savedinstancestate);         Setcontentview (R.layout.activity_main);         button botton1= ( Button) This.findviewbyid (R.ID.BTN1);         edittext= (EditText) This.findviewbyid (R.id.edit);         textview= (TextView) This.findviewbyid (R.ID.MYTEXT1);         botton1.setonclicklistener (new  onclicklistener ()  {            @ override        &Nbsp;   public void onclick (view arg0)  {                 // todo auto-generated method  stub                 Intent intent=new intent (Mainactivity.this,newactivity.class);                 bundle bundle=new bundle ();                 bundle.putstring ("Name",  edittext.gettext (). toString ());                 intent.putextras (bundle);                 startactivityforresult (intent,0);             }         });     }    protected  void onactivityresult (Int requestcode,int resultcode,intent data)  {         bundle bundle=data.getextras ();         textview.settext (bundle.getstring ("name"));     }}

Newactivity:

public class newactivity extends activity {    private  Textview textview;    protected void oncreate (Bundle  savedinstancestate)  {        super.oncreate (savedInstanceState);         setcontentview (r.layout.activity_new);         button botton1= (Button) This.findviewbyid (R.ID.BTN2);         textview= (TextView) This.findviewbyid (R.ID.MYTEXT3);         intent intent=getintent ();        bundle  Bundle=intent.getextras ();         textview.settext (Bundle.getString (" Name "));         botton1.setonclicklistener (New onclicklistener ()  {             @Override              public void onclick (view arg0)  {                 // TODO Auto-generated method stub                 intent intent= New intent ();                 bundle bundle=new bundle ();                 bundle.putstring ("name",  "I ' m from new");                 intent.putextras (bundle);                 newactivity.this.setresult (0,intent);                newactivity.this.finish ();             }         });     }}


The effect is as follows:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/59/D2/wKioL1TrJMuTkoN9AACl-JCgfbE771.jpg "style=" float: none; "title=" 1.png "alt=" wkiol1trjmutkon9aacl-jcgfbe771.jpg "/>650) this.width=650; src= http://s3.51cto.com/ Wyfs02/m01/59/d6/wkiom1tri8xqaufiaacv7gr14dk739.jpg "style=" Float:none; "title=" 2.png "alt=" Wkiom1tri8xqaufiaacv7gr14dk739.jpg "/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/59/D2/wKioL1TrJMyRPHGyAACb4NtUzaQ582.jpg "style=" float: none; "title=" 3.png "alt=" Wkiol1trjmyrphgyaacb4ntuzaq582.jpg "/>


request code and result code

Request Code : When a different business opens the same activity, the request code is used to determine the open request issued by that business.

For example, when two buttons in a mainactivity open Activity1, you can set different request codes, and then in Activity1, you can determine whether button1 or button2 are turned on according to different request codes.


Result code : When more than one activity is opened in the same business, the result code is used to determine the parameter returned by the activity.

For example, when a button in Mainactivity opens Activity1,activity2,activity3, you can set different result codes in three activity. Then, in the method Onactivityresult in Mainactivity, judging by the result code, the results are returned by the activity.


This article is from the "Useless Uncle" blog, please be sure to keep this source http://aslonely.blog.51cto.com/6552465/1615013

Transfer of information between Android activity

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.