您的位置:首页 > 移动开发 > Android开发

Understanding the Android Life Cycle

2013-06-21 15:17 288 查看
Problem

Android apps do not have a “main” method; you need to learn how they get started and how they stop or get stopped.

Solution

The class android.Activity provides a number of well-defined life-cycle methods that are called when an application is started, suspended, restarted, and so on, as well as a method you can call to mark an activity as finished.

Discussion

Your Android application runs in its own Unix process, so in general it cannot directly affect any other running application. The Dalvik VM interfaces with the operating system to call you when your application starts, when the user switches to another application, and so on. There is a well-defined life cycle for Android applications.

An Android application has three states it can be in:

• Active, in which the app is visible to the user and is running

• Paused, in which the app is partly obscured and has lost the input focus

• Stopped, in which the app is completely hidden from view

Your app will be transitioned among these states by Android calling the following methods on the current activity at the appropriate time:

void onCreate(Bundle savedInstanceState)

void onStart()

void onResume()

void onRestart()

void onPause()

void onStop()

void onDestroy()



For an application’s first activity, onCreate() is how you know that the application has been started. This is where you normally do constructor-like work such as setting up the “main window” with setContentView(), adding listeners to buttons to do work (including starting additional activities), and so on. This is the one method that even the simplest Android app needs.

You can see the effects of the various life cycle methods by creating a dummy project in Eclipse and overriding all the methods with log “debug” statements.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: