The example in this article describes the way Android implements data transfer between activity based on intent. Share to everyone for your reference, specific as follows:
Mainactivity:
Package Com.test.intentdemo;
Import android.app.Activity;
Import android.content.Intent;
Import Android.net.Uri;
Import Android.os.Bundle;
Import Android.view.Menu;
Import Android.view.View;
Import Android.widget.Button;
public class Mainactivity extends activity {private Button btn=null; The public void OnCreate (Bundle savedinstancestate)//oncreate method is used to initialize the activity instance object {super.oncreate (savedinstancestate); The role of/super.oncreate (Savedinstancestate) is to invoke the OnCreate method of its parent activity to achieve drawing work on the interface Setcontentview (r.layout.activity_
Main); The function of//setcontentview (R.layout.main) is to load an interface btn= (Button) Findviewbyid (R.ID.BTN);
Btn.setonclicklistener (listener);
Private View.onclicklistener listener=new View.onclicklistener () {@Override public void OnClick (View v) {//Intent intent=new Intent ();//Intent.setaction (intent.action_sendto);//Intent.setdata (Uri.parse ("Smst
o:5554 ")); Intent.putextra ("Sms_body", "hello!"); /sms_body can not be freely replaced by//startactivity (intENT);
Intent intent=new Intent (); Intent.setclass (Mainactivity.this, Secondactivity.class)//jump from one activity to another activity Intent.putextra ("str", "
Intent demo ")//Add additional data to Intent, key is" str ", key value is" Intent Demo "startactivity (Intent);
}
};
@Override public boolean Oncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.activity_main, menu);
return true;
}
}
Secondacitivity:
Package Com.test.intentdemo;
Import Android. r.string;
Import android.app.Activity;
Import android.content.Intent;
Import Android.os.Bundle;
Import Android.widget.TextView;
public class Secondactivity extends activity
{
private TextView secondtxt;
@Override
protected void onCreate (Bundle savedinstancestate)
{
//TODO auto-generated method stub
super.oncreate (savedinstancestate);
Setcontentview (R.layout.second);
Intent intent=getintent ()//getintent retrieves the original Intent contained in the project, assigns the retrieved Intent to a variable of Intent type Intent Bundle
Intent.getextras ();//.getextras () gets additional data
String str=bundle.getstring ("str") attached to intent;//getstring () Returns the value of the specified key
secondtxt= (TextView) Findviewbyid (r.id.secondtxt);//TextView Display value
secondtxt.settext (str);
}
For more information on Android-related content readers can view the site: "The activity of Android programming skills Summary", "Android Development introduction and Advanced Course", "Android Resource Operation skills Summary", "Android File operating skills summary" , "Android Operation SQLite Database Skills Summary", "Android operation JSON format Data Skills summary", "Android Database Operation skills Summary", "Android programming development of SD card operation method Summary", " Android View tips Summary and a summary of the use of Android controls
I hope this article will help you with the Android program.