Android app Activity parameters and redirection

Source: Internet
Author: User

Acitvity Development

A. Configuration File

 
 
  1. <activity android:name=".OtherActivity" android:label="otherActivity"> 
  2. </activity> 

B. Write layout files

C. Write a class to inherit the activity and override onCreate. In this method, set the layout file.SetContentView (R. layout. other );

Activity Parameters// Method 1 for parameter transfer 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 meaning of activating it

// Method 1 get the parameter String name = intent. getStringExtra ("name"); int age = intent. getIntExtra ("age", 0); // method 2 get the Parameter
Int age = intent. getExtras (). getInt ("age ");
String name = intent. getExtras (). getString ("name ");

Set the component to be activatedIntent intent = new Intent (MainActivity. this, OtherActivity. class); // set the component to be activated for intent // Method 1: intent. setClass (MainActivity. this, OtherActivity. class); // set the component to be activated // Method 2: intent. setComponent (new ComponentName (MainActivity. this, OtherActivity. class); // set the component to be activated

When an opened activity is closed, the following method is used to open the activity:

 
 
  1. StartActivityForResult (intent, 10); // is the request 10 when the opened intent is disabled?

Intent intent2 = new Intent (); intent2.putExtra ("result", "this is the result of disabling returned"); setResult (10, intent2 ); // 10 is the return code finish (); // close the activity

Override of opened activity

 
 
  1. /**
  2. * When an opened activity is disabled
  3. * RequestCode Request Code
  4. * ResultCode return code
  5. * The Request Code and return code are used to differentiate multiple activities during a jump.
  6. */
  7. @ Override
  8. Protected void onActivityResult (int requestCode, int resultCode, Intent data ){
  9. Toast. makeText (this, data. getStringExtra ("result"), Toast. LENGTH_SHORT). show ();
  10. Super. onActivityResult (requestCode, resultCode, data );
  11. }

Android Study Notes: Activity jump

Insert a new Activity in Android Development

Android development: Passing values between activities

Android Application Development Tutorial: communication between two running activities

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.