Intent object preliminary

Source: Internet
Author: User

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

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.