Intent, intent

Source: Internet
Author: User

Intent, intent

1. Use show Intent

Intent intent = new Intent (FirstActivity. this, SecondActivity. class );

StartActivity (intent );

The above code is used to open the SecondActivity.

2. Use implicit Intent

First open AndroidManifest. xml and add the Code:

<Activity
Android: name = "com. example. activitytest. SecondActivity"
Android: label = "@ string/title_activity_second">
<Intent-filter>
<Action android: name = "com. example. activitytest. ACTION_START"/>
<Category android: name = "android. intent. category. DEFAULT"/>
<Category android: name = "com. example. activitytest. MY_CATEGORY"/>
</Intent-filter>
</Activity>

Then modify the Click Event of the button in FirstActivity:

Btn. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
Intent intent = new Intent ("com. example. activitytest. ACTION_START ");

Intent. addCategory ("com. example. activitytest. MY_CATEGORY ");

StartActivity (intent );
}
});

You can also use implicit Intent to start activities of other programs.

Intent intent = new Intent (Intent. ACTION_VIEW );
Intent. setData (Uri. parse ("http://www.cnblogs.com/zhouhb "));
// Intent intent = new Intent (Intent. ACTION_DIAL );
// Intent. setData (Uri. parse ("tel: 12345 "));

3. Use Intent to transmit data between activities

3.1 transmit data from FirstActivity to SecondActivity

String s = "from first ";
Intent intent = new Intent (FirstActivity. this, SecondActivity. class );
Intent. putExtra ("data", s );
StartActivityForResult (intent, 1 );

3.2 SecondActivity receives transmitted data

Button btn = (Button) findViewById (R. id. button2 );
Intent intent = getIntent ();
String string = intent. getStringExtra ("data ");
Btn. setText (string );

3.3 SecondActivity returns data to FirstActivity

Btn. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View arg0 ){
// TODO Auto-generated method stub
ReturnData ();
}
});

Private void returnData (){
Intent intent = new Intent ();
Intent. putExtra ("returnData", "from Second ");
SetResult (RESULT_ OK, intent );
Finish ();
}

// If you press Back to return to FirstActivity instead of clicking the button, override onBackPressed

Public void onBackPressed (){
// TODO Auto-generated method stub
ReturnData ();
}

3.4 rewrite onActivityResult in FirstActivity to get the data returned by SecondActivity

Protected void onActivityResult (int requestCode, int resultCode, Intent data ){
// TODO Auto-generated method stub
Switch (requestCode ){
Case 1:
If (resultCode = RESULT_ OK ){
String string = data. getStringExtra ("returnData ");
Toast. makeText (this, string, Toast. LENGTH_SHORT). show ();
}
Break;

Default:
Break;
}
}

Related Article

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.