Getting started with Android. The basic concept of the Intent object and passing data between two activities


Basic Concepts of Intent Objects


is an application component for android


The important content of Intent action (action) and data (data)


========================


Basic usage of Intent objects



================


How to pass data between Activities using Intent


Use the putExtra() series of methods to store data to the Intent object
Use the getxxxExtra() series of methods to retrieve data from the Intent object


//The first activity, place data
public void onClick(View v) {
			Intent intent=new Intent();//First generate the intent object

			intent.setClass(MainActivity.this, MyActivity.class);
			//setClass, the first parameter packageContext, Activity is a subclass of Context, so it can be upcast
			//In short, the first parameter, pass the activity object in
			
			//The second parameter cls. which activity do you want to start
			
			intent.putExtra("org.dick.lifecycle.Name", "Dick");
			// put data. The first parameter is the complete package name + variable name
			
			startActivity(intent);
		}




//Another activity, fetch data
public class MyActivity extends Activity {

	private TextView textView1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate (savedInstanceState);
		setContentView(R.layout.my_activity);

		Intent intent = getIntent();
		String nameString = intent.getStringExtra("org.dick.lifecycle.Name");
		// get data

		textView1 = (TextView) findViewById(R.id.tv1);
		textView1.setText(nameString);//Put the data taken from the activity into the TextView for display
	}

}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327010093&siteId=291194637