Android Hundred Day Program: Life of activity

Source: Internet
Author: User

There are 7 functions in the lifetime of the activity of Android 4, becoming very complex and perfect. So take control of the various functions of activity and know what logic code should be written in that function.

This blog post writes a small program to test how the activity's declaration cycle works.


First summarize:

1 onCreate (): Activyt The first time the creation of the call, but also may be in the activity in OnStop or Onpauce state is automatically released by the system, then when the user needs to continue to change activity, This oncreate () function is also called

2 OnStart (): When the activity changes to the user's visual, then call this function, this function will be more frequent than the OnCreate () call, call the OnCreate () function followed by the inevitable call OnStart (), and in OnStop State, Back to the activity, the OnCreate () function is called, and the OnCreate () function is not called at this time.

3 Onresume (): When activity starts interacting with the user, the call is usually followed by the OnStart () call, so this function is more frequent than the OnStart () function call, and in Onpauce State, This time, if you go back to the activity interface, call Onresume () directly, without calling OnCreate (), and of course you don't have to call the OnCreate () function.

The above three functions are three functions that an activity must invoke at the beginning of a program.

4 OnPause (): The current activity is paused and is normally activated by another activity, blocking it.

5 OnStop (): The current activity is out of sight and will normally be called immediately after OnPause ()

6 OnDestroy (): Called before activity is destroyed, memory shortages can be destroyed by the system or manually by the user.

7 Onrestart (): When the activity is in the OnStop state, but is not destroyed, the function is called when it is restored, and then called OnStart (), and then Onresume () is called to continue running.


Here's a very clear picture that cites Google:

Source: http://developer.android.com/reference/android/app/Activity.html



After creating a new project, use the following code:

Package Su.lifeactivity.lifecycleofactivity;import Android.app.activity;import Android.app.alertdialog;import Android.content.dialoginterface;import Android.os.bundle;import Android.util.log;import Android.view.Menu;import Android.view.menuitem;import Android.view.view;import Android.widget.button;public class MainActivity extends    Activity {private String TAG = "LifeCycle";p rivate button Toastbutton;//call when the activity if first created.        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);                LOG.D (TAG, "in the OnCreate event");        Toastbutton = (Button) Findviewbyid (R.id.toast_button); Toastbutton.setonclicklistener (New View.onclicklistener () {@Overridepublic void OnClick (View arg0) {New Alertdialog.builder (mainactivity.this). Settitle (R.string.dialog). Setitems (r.array.test,new Dialoginterface.onclicklistener () {public void OnClick (Dialoginterface dialoginterface, int i) {}}). Show ();}    });    } @Override public void OnStart () {Super.onstart ();    LOG.D (TAG, "in the OnStart event");    } @Override public void Onrestart () {Super.onrestart ();    LOG.D (TAG, "in the Onrestart event");    } @Override public void Onresume () {super.onresume ();    LOG.D (TAG, "in the Onresume event");    } @Override public void OnPause () {super.onpause ();    LOG.D (TAG, "in the OnPause event");    } @Override public void OnStop () {super.onstop ();    LOG.D (TAG, "in the OnStop event");    } @Override public void OnDestroy () {Super.ondestroy ();    LOG.D (TAG, "in the OnDestroy event"); } @Override Public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu, this adds items to the Actio        n Bar if it is present.        Getmenuinflater (). Inflate (R.menu.main, menu);    return true;   } @Override public boolean onoptionsitemselected (MenuItem item) {     Handle Action Bar Item clicks here.  The action bar would//automatically handle clicks on the Home/up button, so long/As you specify a parent        Activity in Androidmanifest.xml.        int id = item.getitemid ();        if (id = = r.id.action_settings) {return true;    } return super.onoptionsitemselected (item); }}

Using the LOGCAT output test information, open the Logcat to observe the output information.

The specific test is to open the closing program and continue to open other programs when the program opens, such as pressing the phone button. You will see LOG.D (...) The function outputs various hints to logcat.

Where I add a button, click Will pop up an alert dialog, see will not call these functions, the actual test results are not.

Or with a toast test, the result is the same.


A summary of some of the application of the function replication, citing beginning Android 4 application development-seems to be a Chinese-written foreign-published English book, because I see the author's name like Chinese name.

OnCreate (): Create and instantiate objects that need to be used in the program

Onresume (): Starts the code that needs to be run when the activity is in the foreground (for the current interactive activity) or needs service.

OnPause (): The current screen is not visible when the activity, the need to pause the service or code, so that the memory can be saved, you can improve the activity is not closed by the system possible.

OnDestroy (): freeing resources and saving resources


Android Hundred Day Program: Life of 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.