Description: Explores intent objects. This section describes how to use an intent object to transmit data between activities.
Example: data is transmitted to otheractivity and displayed during the jump from mainactivity to otheractivity.
Before proceeding, let's take a look at what intent is.
Basic concepts of intent objects:
1. The intent object is one of the android application components;
2. The intent object represents an intent in the Android system;
3. The most important content of intent is action and data.
Step: 1. generate an intent object in mainactivity, and use the putextra () Series Method in the intent object to put the data into the intent object.
Put a button in activity_main.xml and click to jump to otheractivity.
<Button Android: Id = "@ + ID/btnid" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "start second activity"/>
Generate an intent and use putextra () to store data in the intent object.
package com.away.b_04_intent;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity { private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button=(Button)findViewById(R.id.btnId); button.setOnClickListener(new ButtonListener()); } class ButtonListener implements OnClickListener{@Overridepublic void onClick(View v) {Intent intent = new Intent();intent.setClass(MainActivity.this, OtherActivity.class);intent.putExtra("com.away.b_04_intent.Age", 20);intent.putExtra("com.away.b_04_intent.Name", "Away");startActivity(intent);} }}2. Use the getxxxextra () series methods in otheractivity to retrieve data from intent objects.
package com.away.b_04_intent;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.widget.TextView;public class OtherActivity extends Activity {private TextView textView;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.other);Intent intent = getIntent();int age = intent.getIntExtra("com.away.b_04_intent.Age", 10);String name = intent.getStringExtra("com.away.b_04_intent.Name");textView = (TextView) findViewById(R.id.textView);textView.setText(name + ":" + age + "");}}:
National Day passed... I have to go to work again tomorrow... I really want to go to work at home in the future, but the whole person at home will become very lazy again...
Intent object preliminary