Activity jump & amp; Bundle transfers data, androidactivity jump

Source: Internet
Author: User

Activity jump & Bundle transfer data, androidactivity jump

There are two redirect methods for an Activity:

1. startActivity (Intent intent), directly start other activities

btnChange = (Button)findViewById(R.id.btnChange);btnChange.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubIntent mIntent = new Intent();mIntent.setClass(MainActivity.this, AnotherActivity.class);startActivity(mIntent);}});

2. startActivityForResult (Intent intent, int requestCode), start the Activity with the specified request code, and the program will call onActivityResult after starting the new Activity to obtain the results passed back by the new Activity.

2.1-Bundle: putExtras (Bundle data)/getExtras (), putExtra (String name, Xxx value)/getXxxExtra (String name)

I. the btn in MainActivity calls startActivityForResult:

btnChange = (Button)findViewById(R.id.btnChange);btnChange.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubIntent mIntent = new Intent(MainActivity.this, AnotherActivity.class);startActivityForResult(mIntent,0);}});
II. In MainActivity, rewrite the data returned by the onActivityResult listener.

@ Overridepublic void onActivityResult (int requestCode, int resultCode, Intent intent) {if (requestCode = 1 & resultCode = 1) {Bundle data = intent. getExtras (); // result is the valueString result = data whose key is Name returned by AnotherActivity. getString ("Name ");}}
III. Send data in AnotherActivity

BtnSend = (Button) findViewById (R. id. btnSend); btnSend. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {Intent intent = getIntent (); intent. putExtra ("Name", "John"); // sets ResultCodeAnotherActivity. this. setResult (0); AnotherActivity. this. finish ();}});




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.