Use intent to pass data between activity in Android

Source: Internet
Author: User

simple use of intent transfer data in android:

1. Pass data using intent:

first put the data that needs to be passed into the intent

Intent Intent = new Intent (mainactivity.this,otheractivity.class);

Intent.putextra ("name", " passing Data using intent");

StartActivity (Intent);

then receive the data in the target activity:

Intent intent=getintent ();

Intent.getstringextra ("name")// Get Data

2. use intent to pass static variables:

a static variable is set in the target activity, the data is stored in a static variable when the target activity is started, and then read directly in the target activity.

Intent Intent = new Intent (Mainactivity.this,

Other2activity.class);

Other2activity.data = " pass static variable with intent";

StartActivity (Intent);

3. passing global variables using intent

New Class MYAPP Inherits application, specifying the Name property of the application node in the Androidmanifest.xml file as MYAPP (android:name= ". MyApp ")

public class MyApp extends application {

Publicstring data;

Publicstring GetData () {return data; }

Public Voidsetdata (String data) {This.data =data; }

@Override public void OnCreate () {

Super.oncreate ();

SetData ("Hello World");

} }

}

writes data to the global variable when the target activity is started :

Intent Intent = new Intent (mainactivity.this,other3activity.class);

MyApp MyApp = (MyApp) getapplication ();

Myapp.setdata (" use intent to pass global Variables ");

StartActivity (Intent);

To receive data in the target activity:

MyApp myapp= (MyApp) getapplication ();

Myapp.getdata ();

4. using the Clipboard to pass data

Write the data that you want to pass to the Clipboard:

Intent Intent =new Intent (Mainactivity.this,

Clipboardactivity.class);

Clipboardmanagerclipboardmanager= (Clipboardmanager) Getsystemservice (MainActivity.this.CLIPBOARD_SERVICE);

Clipboardmanager.settext (" using the Clipboard to transmit data ");

StartActivity (Intent);

and then in the target Activity value in:

Clipboardmanager clipboardmanager= (Clipboardmanager) Getsystemservice (Clipboardactivity.clipboard_service);

Clipboardmanager.gettext ();

above is some simple activity data transfer. Besides, I'm at the end of the development app will use some app test the online automated test tool: www.ineice.com


Use intent to pass data between activity in Android

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.