Getting started with Android: intent (2)

Source: Internet
Author: User
Document directory
  • 0. Create a new activity
  • 1. The simplest way to jump to a new activity
  • 2. Redirection with return values

 

1. Intent and activity applications

 

0. Create a new activity

 

It is common to create a new acitivity in an application;

Step 1: Create a class to inherit the activity

 

Step 2: add the <activity> element to the Android-manifest.xml

 

For example, if you have created an activity named subactivity, you must declare it as follows:

        <activity android:name=".SubActivity"></activity>

 

1. The simplest way to jump to a new activity

Program Description: The mainactivity jumps to subactivity after clicking the button. The mainactivity transmits a (name, xiazdong) to subactivity and obtains the display;

 

The effect is as follows:

 

Click the button:

 

Intentactivity. Java

Package Org. xiazdong; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; public class intentactivity extends activity {private button btn1; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); btn1 = (button) This. findviewbyid (R. id. brn1); btn1.setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view v) {intent = new intent (); intent. setclass (intentactivity. this, subactivity. class); // jump from intentactivity to subactivityintent. putextra ("name", "xiazdong"); // put the data startactivity (intent); // start to jump }});}}

Subactivity. Java

Package Org. xiazdong; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. widget. textview; public class subactivity extends activity {private textview TV1; @ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); this. setcontentview (R. layout. sub); TV1 = (textview) This. findviewbyid (R. id. TV1); intent = This. getintent (); // get the current intent bundle = intent. getextras (); // get all data string value = bundle. getstring ("name"); // obtain the value tv1.settext (value );}}

 

2. Redirection with return values

 

Jump to the new activity and pass the returned value to the original activity

Program Description: After the mainactivity jumps to the subactivity, a result code is returned to the intentactivity after the subactivity is executed to execute the corresponding process;

Program effect:

After clicking jump, execute the second activity and then jump back to the first activity.

 

Intentactivity. Java

package org.xiazdong;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;public class IntentActivity extends Activity {private Button btn1;private TextView tv1;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        btn1 = (Button)this.findViewById(R.id.brn1);        tv1 = (TextView)this.findViewById(R.id.tv2);        btn1.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent();intent.setClass(IntentActivity.this, SubActivity.class);startActivityForResult(intent, 100); //requestcode=100}});    }@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);if(requestCode==100&&resultCode==200){Bundle bundle = data.getExtras();String response = bundle.getString("response");tv1.setText(response);}}    }

 

Subactivity. Java

 

Package Org. xiazdong; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. widget. textview; public class subactivity extends activity {private textview TV1; @ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); this. setcontentview (R. layout. sub); TV1 = (textview) This. findviewbyid (R. id. TV1); intent = new intent (); // create an intentintent. putextra ("response", "from 2"); setresult (200, intent); // The return code is 200 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.