Fourth three cornerstones of Android development-activity, service and handler (3)

Source: Internet
Author: User

4.1.5Activity Interactive-activity Jump

In general, our applications will not be as simple as just one interface, but there will be a lot of interfaces, and this time we will create multiple activities and then jump from one activity to another based on the business logic.

We can switch the layout of the mobile phone page between the conversion. But if the page to be converted is not just the background, the color or the text content, but the entire activity needs to be replaced, and the main control to the new activity, it is not only by changing the layout can be completed, We need to jump and data transfer in these activity.

Activity jumps are also very simple to implement, and you can use intent to implement activity jumps within the application. We are more commonly used in two kinds. One is a simple jump, that is, after jumping after the activity before jumping, the other is to jump to the next activity, and wait for its return results to do related operations.

Let's take a simple example to illustrate this.

1) General Jump

Similarly, we create a new Nextactivity.java class that inherits from activity, with only one textview in its layout file, which shows "This is Activity 2".

Suppose we just created the Myactivity.java for the current Activity,nextactivity.java as the next to jump activity. We add a button to the Main.xml layout file, click on the implementation from the Myactivity.java jump to Nextactivity.java effect, the layout file code is as follows:

<?xml version= "1.0" encoding= "Utf-8"?>

<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"

android:orientation= "Vertical"

Android:layout_width= "Fill_parent"

android:layout_height= "Fill_parent" >

<textview

Android:layout_width= "Fill_parent"

android:layout_height= "Wrap_content"

Android:text= "This is Activity 1"/>

<button

Android:id= "@+id/my_button"

Android:layout_width= "Wrap_content"

android:layout_height= "Wrap_content"

android:text= "Jump"/>

</LinearLayout>

Then, let's look at the code in MyActivity:

Private Button btn;

@Override

public void OnCreate (Bundle savedinstancestate) {

Super.oncreate (savedinstancestate);

Setcontentview (R.layout.main);

BTN = (Button) Findviewbyid (R.id.my_button);

Btn.setonclicklistener (New View.onclicklistener () {

@Override

public void OnClick (View v) {

Intent Intent = new Intent ();

Intent.setclass (Myactivity.this, Nextactivity.class);

StartActivity (Intent);

}

});

}

Let's run one down and look at the effect, 4-4 shows:

Figure 4-4 Activity jump before

Below, we click the button. This time, the operation error, the cause of the error is "Android.content.ActivityNotFoundException", This is because we do not configure nextactivity in the Androidmanifest.xml file, we simply configure:

<activity android:name= ". Nextactivity "android:label=" @string/app_name "/>

Now when you click on the button, effect 4-5 looks like this:

Figure 4-5 after activity jumps

As you can see, the jump has been successful.

--------------------------------------------trying to put an ad, and now there's no work to survive.Ping An Lufax is affiliated to the Ping An group's peer platformannual ROI 7%-9% is the preferred alternative to bank bankingPersonal lessons recommend investing in anxin or guaranteed rainbow projectsdon't invest in an e that's almost impossible to transfer it's very difficult to withdraw in advanceRegistration LinkHttp://affiliate.lufax.com/action/36XBUSign up with this link and I'll have dozens of bucks for the extra cash bonus.--------------------------------------------

2) Jump and return value

There may be times when a general jump does not meet our needs. For example, we fill out a form, after the submission of an error, we go back and want to keep the previously filled data, so that only simple jump can not be satisfied.

Next, let's look at another way to jump. In this example, we want to implement a jump from myactivity to nextactivity, and when MyActivity receives the "information" returned by nextactivity, the message is displayed in MyActivity, and the jump code in MyActivity is as follows:

Intent Intent = new Intent ();
Intent.setclass (Myactivity.this, Nextactivity.class);

Startactivityforresult (Intent, 1);

This jumps from myactivity to nextactivity and passes in a request code "1". At the same time, we need to rewrite the Onactivityresult method in the activity in Myactivity.java to receive the return code in the Nextactivity.java code as follows:

    protected void Onactivityresult (int requestcode, int resultcode, Intent data) {

         switch (resultcode) {

        case RESULT_OK:

            //When return code is Result_ OK when related operation

        if (Requestcode = = 1) {

           }

             break;

       }

It is important to note that the ResultCode is equivalent to a switch, and when the switch in the Nextactivity.java is turned on, it is processed accordingly.

To modify the OnCreate method for nextactivity:

@Override

protected void OnCreate (Bundle savedinstancestate) {

Super.oncreate (savedinstancestate);

Setcontentview (R.layout.next);

Setresult (RESULT_OK);

Finish ();

}

Below, let's look at the effect, shown in 4-6:

Figure 4-6 return value after activity jump

4.1.6 data transfer in activity

Most of the time, we're not just doing activity jumps, we're transferring data while doing activity jumps, Here we can use the Android.os.Bundle object to close the data, the data or parameters to pass through the bundle to pass the data between different activity. Also take the previous code as an example, we want to jump from the current activity to the next activity, and pass in a double parameter, a string parameter, in the Myactivity.java code as follows:

Intent Intent = new Intent ();
Intent.setclass (Myactivity.this, Nextactivity.class);

Bundle bundle = new bundle ();

Double height = 1.74;

String name = "Li Lei";

Bundle.putdouble ("height", height);

Bundle.putstring ("name", name);

Intent.putextras (bundle);
StartActivity (Intent);

If one party sends a parameter, one must receive the parameter. Its reception is also very simple to implement, in Nextactivity.java the code is as follows:

Bundle bundle = NextActivity.this.getIntent (). Getextras ();

Double height = bundle.getdouble ("height");

String name = bundle.getstring ("name");

Experience Sharing:

It is important to note that when Startactivityforresult is executed, theRequestcode value needs to be >=0, otherwise startactivityforresult becomes startactivity; In such a case, suppose there are now two activity:a and B. Activity a uses Startactivityforresult to jump to activity B, and after tracking found that activity B to jump is not immediately started, but instead directly executes activity A's Onactivityresult method, The reason is that the launchmode of activity B to start is set to Singletask, and this time it executes the Onactivityresult method before activating the activity, so we don't get the result we want.

Fourth three cornerstones of Android development-activity, service and handler (3)

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.