Several ways to jump Android activity

Source: Internet
Author: User

1. Explicitly calling methods

Intent Intent = new Intent (this, otheractivity.class);//method 1Intent Intent2 = new Intent (); Intent2.setclass (This, Otheractivity.class);//Method 2intent2.setclassname (This, "com.mh.MainActivity.OtherActivity");//Method 3, This method can be used to open other application intent2.setcomponent (new ComponentName (this, otheractivity.class));//Method 4startActivity (Intent2);

2. The implicit invocation method (as long as the action, category, data, and activity to jump to are set in Androidmanifest.xml, OK

<activity android:name= "com.mh.MutiActivity.OtherActivity" >    <Intent-filter>        <action Android:name= "Com.mh.test.action"/>        <category android:name= "Com.mh.java"/>        <!-- Because the category is built into the startactivity () method, the index must be added to this category otherwise Android.intent.category.DEFAULT cannot jump--        <category android:name= " Android.intent.category.DEFAULT "/>        <!--set which, then those must match, not set can be arbitrarily written---        <data android:scheme=" Schemename "android:host=" mh.com "android:path="/introduction "/>        <!--data Type--        <data android: Mimetype= "Txt/plain"/>    <intent-filter></activity>

3. After jumping to another activity, you can return data when you return

A. On the activity side of the jump, call Startactivityforresult (Intent2, 1) jumps to the next activity, where the first parameter is the incoming intent object, and the second is the set request code;

B. After jumping to the second activity, call the Setresult (intent) method to return to the previous activity, where the first parameter is the result code and the second is the intent object passed in

C. Obtaining the returned data in the first activity through the Onactivityresult () method

--------------------------------------------------------------

Startactivityforresult (Intent Intent, int requestcode)
Onactivityresult (int requestcode, int resultcode, Intent data)
Setresult (int resultcode, Intent data)
These three functions, especially pay attention to their parameters!
Now there are two activities A and B, if we jump from A to B, then B after completing the corresponding work, finish off B, then pass the data to A,a after receiving the data to do the corresponding operation. We can use three of these functions:
A:
Start B
Intent Intent = new Intent ();
Intent.setclass (A.this, B.class);
Startactivityforresult (Intent, 2);//2 is our own definition of constants, corresponding to the resultcode used below
B:
Do something
Setresult (2, NULL);
Finish ();
After the above main code is completed we need to override the Onactivityresult () function in a:
A:
@Override
protected void Onactivityresult (int requestcode, int resultcode, Intent data) {
Super.onactivityresult (Requestcode, ResultCode, data);
if (2 = = Requestcode) {
Do something
}
}

in more detail:
Startactivityforresult and Onactivityresult
Jumps between androidactivity are not just startactivity (Intent i), Startactivityforresult (Intent Intent, int requestcode) are also common methods.
The function is to use the Onactivityresult (int requestcode, int resultcode, Intent data) method to obtain the operation after the request activity has ended.
There are three methods to note: Startactivityforresult (Intent Intent, int requestcode), onactivityresult (int requestcode, int resultcode, Intent data), Setresult (int resultcode, Intent data)

For example, the following code: from jump to Tob and TOC
From:
if (condition) {
Intent Intent = new Intent (this, tob.class);
Startactivityforresult (Serverintent, request_code_01);//Jump to Tob
}else{
Intent Intent = new Intent (this, toc.class);
Startactivityforresult (Serverintent, request_code_02);//Skip to TOC
}

public void Onactivityresult (int requestcode, int resultcode, Intent data) {
Switch (requestcode) {
Case REQUEST_CODE_01:
if (RESULTCODE==ACTIVITY.RESULT_OK)
Do something
Break
Case REQUEST_CODE_02:
Do something
Break
}
}

Here's the argument for Startactivityforresult (Intent Intent, int requestcode), the first Intent needless to say, the second one needs attention, we use Request_code_ 01 and Request_code_02, in fact, this is our own definition of an int type constant, for the purpose of marking, the specific role can be seen in the Onactivityresult method, to determine which activity is returned from.

ToB:
Intent Intent = new Intent ();
Intent.putextra (key, value);
Setresult (ACTIVITY.RESULT_OK, intent);
Finish ();//after which the results are passed back to the from

Toc:
Intent Intent = new Intent ();
Intent.putextra (key, value);
Setresult (ACTIVITY.RESULT_OK, intent);
Finish ();//after which the results are passed back to the from

The first parameter of the Setresult corresponds to the second parameter of the onactivityresult above, note that the first parameter of Onactivityresult is confused with the second parameter, one is the request token and the other is the return token.

Several ways to jump from Android activity

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.