About the four components in Android (Activity)

Source: Internet
Author: User

I. Summary of activity

Activity is the component in the Android app that is responsible for interacting with the user. Create your own activity in your app to inherit activity or to inherit activity


Sub-class.


public class Testactivity extends Activity {}


Ii. Use of activity


As you can see from Figure 1.1, the activity indirectly inherits the base classes of Contextthemewrapper, Contextwrapper, Context, and object, so


The activity can call their methods directly. Of course it doesn't make any sense to inherit activity, and we also need to implement one or more of the activity's


Method.


Figure 1.1


OnCreate (Bundle savedinstancestate) method is most commonly used in development, this method is called back when activity is created, we can


The view that needs to be displayed in order to load through the Setcontentview method. Three Setcontentview methods are provided in the activity, as shown in Figure 1.2


Figure 1.2

(1) public void Setcontentview (int layoutresid): Load the layout ID from the resource file


(2) public void Setcontentview (view view): Loads a view into the activity.


(3) public void Setcontentview (view view, viewgroup.layoutparams params): Loads a specified view into activity


in, and set layout parameters with params.


Once we have created the activity we need, we also need to configure it in Androidmainfest.xml.


<activity android:name= "Com.example.TestActivity" >

When your activity is created, we need to start or destroy it, and the activity can be started using the following two methods:


(1)publicvoid startactivity (Intent Intent): Start activity.


(2)publicvoid startactivityforresult (Intent Intent,intrequestcode) : Starts the activity with the specified request code Requestcode


When the activated activity returns, the result can be obtained through the onactivityresult (intrequestcode,int resultcode, Intent data) method.


To destroy an activity, you can use the following methods:


(1)publicvoid finish (): Ends the current activity.


(2)publicvoid finishactivity (intrequestcode): Ends with Startactivityforresult ( intentintent,int requestcode) method start


The dynamic activity.



Iii. data transfer between the activity


When we start another activity from one activity, we need to pass some data to the past, when we need to use intent, we can use


Intent provides multiple overloads of the Putextra method for delivery, as shown in Figure 1.3



Figure 1.3



As can be seen from figure 1.3, there are various types of data that can be passed through intent, the corresponding can call intent's get method by the method in Figure 1.4,


The appropriate data to be taken.



Figure 1.4



Iv. life cycle of activity


From activity creation to destruction, the status of the activity can be broadly divided into the following four categories:


(1) Active status: The current activity is located in the foreground, the user is visible, you can get the focus.


(2) Suspend status: Other activity is located in the foreground, the activity is still visible, but the focus is not available.


(3) Stop state: The activity is not visible and loses focus.


(4) Destruction Status: The activity ends, or the Dalvik process where the activity is located is ended.


See figure 1.5 for the life cycle of the activity:




Figure 1.5




public class Activity extends ApplicationContext {     protected void onCreate (Bundle savedinstancestate);     protected void OnStart ();          protected void Onrestart ();     protected void Onresume ();     protected void OnPause ();     protected void OnStop ();     protected void OnDestroy (); }

OnCreate (bundlesavedinstancestate): callback when creating activity. The method will only be called once.


OnStart (): Callback when activity is started.


Onrestart (): Callback when activity is restarted.


Onresume (): Callback When the activity is resumed, the OnStart () method will definitely callback the Onresume () method.


OnPause (): Callback when activity is paused.


OnStop (): Callback when activity is stopped.


OnDestroy (): Callback when activity is destroyed. The method will only be called once.

As you can see from Figure 1.5, the sequence in which an activity normally starts is the order in which they are called onCreate-onstart-> Onresume.


When the activity is killed, the order is onpause-> onStop--OnDestroy, which is a complete life cycle.


If a new activity is aborted when it is full screen, the order of the call is Onpause->onstop, and the recovery time is onstart->onresume.


If the application is interrupted by an activity that is theme translucent or dialog, then it is only OnPause, the time of recovery


Onresume.



Figure 1.6


As can be seen from Figure 1.6, Onpause,onstop, OnDestroy, three states of activity are likely to be killed by the system, to ensure that the program is


OnPause (), you will write the code in the persistence layer operation to save the user's edits to the storage media (usually the database). Look


Examples from the official website:


public class Calendaractivity extends Activity {     ...     static final int day_view_mode = 0;     static final int week_view_mode = 1;     Private Sharedpreferences mprefs;     private int mcurviewmode;     protected void OnCreate (Bundle savedinstancestate) {         super.oncreate (savedinstancestate);         Sharedpreferences mprefs = Getsharedpreferences ();         Mcurviewmode = Mprefs.getint ("View_mode", Day_view_mode);     }     protected void OnPause () {         super.onpause ();          Sharedpreferences.editor ed = Mprefs.edit ();         Ed.putint ("View_mode", Mcurviewmode);         Ed.commit ();     } }

Writes are made using Sharedpreferences.editor in OnPause () and read in OnCreate (Bundle savedinstancestate).














Reprint Please specify source:http://blog.csdn.net/hai_qing_xu_kong/article/details/47315083 Emotional Control _














Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

About the four components (Activity) in Android

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.