Android Common components (one of activity learning)

Source: Internet
Author: User

About activity This article summarizes the learning of the jump between pages, two ways, as well as passing parameters and receive return values.

About jump to the next page, two activity between the intent to communicate, two ways:

startActivity();startActivityForResult();

First, open the next page

Intent intent=new Intent(this, NewActivity.class);    // 下面是另外3种设置激活组件的方法    //intent.setClass(this, NewActivity.class);    //intent.setClassName(this, "com.example.mulactivity.NewActivity");    //intent.setComponent(new ComponentName(this, NewActivity.class));        startActivity(intent);

2. Pass parameters to the next page
Method of delivery:

    publicvoidOpenNewActivity(View v){        Intent intent=new Intent(this, NewActivity.class);        //第一种传值方式        //intent.putExtra("name", "atm");        //intent.putExtra("age", 10);        //第二种传值方式         Bundle bundle=new Bundle();         bundle.putString("name""atm");         bundle.putInt("age",10);         intent.putExtras(bundle);         startActivity(intent);

The next page receives the method:

Intent intent=getintent ();The first method of receiving data//string name=intent. Getstringextra("Name");Integer age=intent. Getintextra("Age",0);The second method of accepting data//bundle Bundle = Intent. Getextras();String name = Bundle. getString("Name");int age = Bundle. GetInt("Age");TextView. SetText(name+"The Age is:"+age);

3. Get the value returned when the next page closes

Overriding the Onactivityresult () method

 Public void opennewactivity(View v) {Intent intent=NewIntent ( This, Newactivity.class); Startactivityforresult (Intent, the); }@Override    protected void Onactivityresult(intRequestcode,intResultCode, Intent data) {//TODO auto-generated method stubString res = Data.getstringextra ("res"); Toast.maketext ( This, res, Toast.length_long). Show ();//super.onactivityresult (Requestcode, ResultCode, data);}

Return a value to the top-level page when the page is closed:

publicvoidCloseActivity(View v){        new Intent();        intent.putExtra("res""这是我返回回来的数据");        setResult(20, intent);        this.finish();//关闭当前的activity    }

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Common components (one of activity learning)

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.