Android Basic finishing four components activity

Source: Internet
Author: User

Recently prepare the system to review the various aspects of Android knowledge, based on the principle of knowledge sharing, I will comb the process of some things to record, right when a study notes it.

Down to the chase .....

What is activity

Activity is one of the four components of Android and one of the most important components. As a component that interacts with the user, we can compare activity to a folder window on a Windows system, an interface that interacts with the user. Further, when we pick up the android to turn on the dial function, the interface that shows the dial is actually an activity, and of course, to a larger extent, any interface displayed on the mobile phone screen is activity.

Three states of activity

Running status : When the activity is at the forefront of the screen, when the actiivity is fully displayed in the user's interface and can get the focus, it can respond to events such as the user's touch screen, which is now running.

paused state : When activity is obscured by other activity, but still partially visible, it is paused, and when it is paused, the activity still updates the UI, but at this point it cannot get the focus, that is, it will not respond to events such as user touch, back, etc.

Stop State : When the activity is completely obscured by another activity, the stop state does not mean that the activity is stopped and is understood to be running in the background. However, if the system needs memory, the activity releasing resources will take precedence over the stopped state, so the important data should be saved when the activity goes to a stopped state.

Life cycle of activity

On the right is the life cycle diagram of the activity, which has a total of seven life cycle methods. The so-called life cycle, is the activity between the various state transitions, the system will be a callback of its various life cycle functions, but also let us according to business needs, rewrite the various life cycle methods to implement software in the various state of business logic.

Start activity to run State:

OnCreate (), OnStart (), Onresume (), the interface is fully displayed

Run state to paused state:

Interface full Display---onPause () interface is partially obscured

Suspend state to run state:

Interface is partially obscured---onresume () interface fully displayed

Run state to stop state:

Interface full display, OnPause (), OnStop (), fully covered

Stop state to run state:

Interface complete Coverage--onrestart (), OnStart (), Onresume (), fully display

Exit activity:

Interface full display, OnPause (), OnStop (), interface full cover, OnDestroy (), Activity end life cycle

PS: When the system memory is tight, the system can release the activity to obtain the resources, priority to release the inactive activity, the second is the activity of the temporary state, and the running state of the acitivity is generally impossible to be forcibly released, Unless there is an extreme lack of resources (this time directly crashes ...) ), when the activity is released, directly into the extinction, it is important to note that OnDestroy () will not be executed, so it is inappropriate to save the data in OnDestroy processing, can be in onsaveinstancestate () The important data are processed accordingly.

Activation of activity

When we want to jump from the current activity to the next activity, we need to master the activation of the activity, referring to the following code:

Intent Intent =NewIntent ( This, Otheractivity.class);//notation 1//Intent Intent = new Intent (Getapplicationcontext (), otheractivity.class);//notation 2//       //Intent Intent = new Intent ();//notation 3//Intent.setclass (this, otheractivity.class);startactivity (intent);//Start another activity       

Intent is the link between four components, starting an activity is typically a new intent instance, specifying the appropriate startup components, such as Otheractivity.class. Then call the Activity method StartActivity (Intent) to notify the Android system to load the otheractivity.

Transfer of data between activity

For the application, it is not enough to start another activity from one activity, and sometimes it is necessary to compare the data information exchange. The Android system provides a number of methods for data communication, and naturally provides a simple way to communicate between activity, see the following code:

Firstactivity transmitting data to otheractivity <data, "transmitted data" >

1Intent Intent =NewIntent ( This, Otheractivity.class);2         3Intent.putextra ("Data", "transmitted");//data is key4 //another way to load data5 //Bundle bundle = new bundle ();6 //bundle.putstring ("Data", "transmitted");7 //Intent.putextras (bundle);8         9StartActivity (Intent);

There are two methods of representation, the first method used in the scenario is to transmit a small amount of simple data, the latter method is to transfer more and more complex data used. But from the bottom up, the first method is the encapsulation of the latter, which still creates a bundle inside intent and puts the data (such as "passed data") into it.

Otheractivity get transferred data <data, "passed data" >

 1  Intent Intent = Getintent ();         2   Gets the method  3  String data = Intent.getstringextra ("data"); // data's value is "passed data", passed in by the previous activity  4  //  Another method  5  //  bundle bundle = Intent.getextras ();        6  //  

The method obtained is usually to obtain the bundle intent carry, and then get the data stored in it through the Get method, and the previous method does not explicitly obtain the bundle code, because Intent.getstringextra (key) In the function is already the first to obtain good bundle and the corresponding key data parsed out, so the two methods are essentially the same, but if a large number of data proposed to use the latter method, because the data from the bundle to obtain more data than from the intent efficiency improved, compared to the mobile era, Efficiency is a very important thing to pay attention to.

Fristactivity starts Otheractivity and obtains the data returned by otheractivity at the end of the

If the business needs to start another activity to return data to the original activity, when the activity is started is called startactivity (intent), but should call Startactivityforresult (intent , Requestcode), Requestcode is an identifier for the activation activity, which is a positive integer value that can be used to represent the data returned by the activity with different identifiers when there are multiple startup activity and different processing. Processing the return data in fristactivity is handled in Onactivityresult (Requestcode, ResultCode, data), while the first parameter, Requestcode, is the identification code entered at startup. See Code

1 New Intent (This, otheractivity.  Class); 2 Startactivityforresult (intent, 0); // set ID of 0

This is the code to start the activity, but we need to rewrite the Onactivityresult (Requestcode, ResultCode, data) in fristactivity for the returned data.

1 @Override2 protected voidOnactivityresult (intRequestcode,intResultCode, Intent data) {3     Super. Onactivityresult (Requestcode, ResultCode, data);4     if(Requestcode = = 0) {//according to the value of Requestcode, the bundle that is actually returned by activity5Bundle bundle =Data.getextras ();6String GetData = bundle.getstring ("name");7     }8}

Then, in the startup otheractivity, rewrite the method that returns the data

    New Intent ();    Intent.putextra ("name", "returned data");    Setresult (1, intent); // Set the intent    to return data Finish (); // end the current activity    

After the general Setresult call finish () to end the current activity, the data is returned to the fristactivity, and the data returned is the time that the Onactivityresult call was called before finish.

End of activity

Finish (): The system will move the activity out of the stack after executing the finish (), but it does not release the resource immediately, nor does it invoke the method that OnDestroy this life cycle.

OnDestroy (): When activity executes to this life cycle, it also means that the activity will be completely released, and if you need to restart the activity, you need to re-oncreate ().

system.exit (0): This thing will kill the whole application, quit the process.

Android Basic finishing four components 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.