Activity parameters and redirection

Source: Internet
Author: User

Acitvity Development
A. Configuration File
<Activity Android: Name = ". otheractivity" Android: Label = "otheractivity">
</Activity>
B. Write layout files
C. Write a class to inherit the activity
Override oncreate
In this method, set the layout file setcontentview (R. layout. Other );

Activity Parameters
// Method 1 for passing parameters with Intent
// Intent. putextra ("name", "Tan Jianping ");
// Intent. putextra ("Age", 25 );

// Method 2
Bundle bundle = new bundle ();
Bundle. putstring ("name", "Tan Jianping ");
Bundle. putint ("Age", 25 );
Intent. putextras (bundle );

Intent intent = This. getintent (); // get the intent to activate it
// Obtain parameters in method 1
String name = intent. getstringextra ("name ");
Int age = intent. getintextra ("Age", 0 );

// Obtain parameters in method 2
Int age = intent. getextras (). getint ("Age ");
String name = intent. getextras (). getstring ("name ");

Set the component to be activated
Intent intent = new intent (mainactivity. This, otheractivity. Class); // set the component to be activated for intent
// Write Method 1: intent. setclass (mainactivity. This, otheractivity. Class); // set the component to be activated
// Statement 2: intent. setcomponent (New componentname (mainactivity. This, otheractivity. Class); // you can specify the component to be activated.

 Returned value when the opened activity is disabled
The method to open an activity is
Startactivityforresult (intent, 10); // is the request 10 when the opened intent is disabled?
 
When the opened activity is closed
Intent intent2 = new intent ();
Intent2.putextra ("result", "this is the result of disabling returned results ");
Setresult (10, intent2); // 10 is the return code.
Finish (); // close the activity

rewrite an opened activity
/**
* When the opened activity is closed
* requestcode Request Code
* resultcode return code
* The Request Code and return code are used to differentiate multiple activities during a jump
*/
@ override
protected void onactivityresult (INT requestcode, int resultcode, intent data) {
toast. maketext (this, Data. getstringextra ("result"), toast. length_short ). show ();
super. onactivityresult (requestcode, resultcode, data);
}

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.