The examples in this article describe the way Android implements communication between activity. Share to everyone for your reference. Specifically as follows:
A system-supplied startactivity (Intent Intent) method can be used to open new activity in an activity, and you can decide whether to pass parameters for new activity before opening a new activity.
First: Open new activity, do not pass parameters
public class Mainactivity extends activity {
@Override public
void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (r.layout.main);
Button btnopen= (button) This.findviewbyid (r.id.btnopen);
Btnopen.setonclicklistener (New View.onclicklistener () {public
void OnClick (View v) {
//new an explicit intent, The first parameter is the current Activity class object, and the second parameter is the activity class startactivity you want to open
(new Intent (Mainactivity.this, Otheractivity.class));
}
});
}
}
Second: Open a new activity and pass several arguments to it
Package com.ljq.activitys;
Import android.app.Activity;
Import android.content.Intent;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.Button; public class Mainactivity extends activity {@Override public void onCreate (Bundle savedinstancestate) {Super.onc
Reate (savedinstancestate);
Setcontentview (R.layout.main);
Button btnopen= (button) This.findviewbyid (R.id.btnopen); Btnopen.setonclicklistener (New View.onclicklistener () {public void OnClick (View v) {//First: Open a new activity, do not pass the parameter Number//Create an explicit intent, the first parameter is the current Activity class object, and the second parameter is the activity class you want to open//startactivity (new Intent (Mainactivity.this, Othera
Ctivity.class));
Second: Open new activity and pass several parameters to it Intent intent=new Intent (Mainactivity.this, Otheractivity.class);
The Bundle class is used for carrying data Bundle bundle=new Bundle ();
Bundle.putstring ("name", "Linjiqin");
Bundle.putint ("Age", 24);
Attached with additional data Intent.putextras (bundle); StartactIvity (Intent);
}
});
}
}
To receive the parameters passed by the previous activity in a new activity
Package com.ljq.activitys;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.util.Log;
public class Otheractivity extends activity {
Private final static String tag= "Otheractivity";
@Override
protected void onCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.other);
In the new activity receive the parameters passed by the preceding activity
Bundle bundle=this.getintent (). Getextras ();
String name=bundle.getstring ("name");
Integer age=bundle.getint ("Age");
LOG.I (TAG, name+ ":" +age);
}
I hope this article will help you with your Android program.