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.
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.
相关文章推荐
- Understanding the Android Life Cycle
- Android官方Training阅读笔记 ---- Managing the Activity Lifecycle(Recreating an Activity) (四)
- Android studio Declaring custom 'clean' task when using the standard Gradle lifecycle解决方法
- Android官方文档翻译 十六 4.Managing the Activity Lifecycle
- Android官方Training阅读笔记 ---- Managing the Activity Lifecycle(Starting an Activity) (一)
- A journey on the Android Main Thread - Lifecycle b
- Understanding the Request Processing Lifecycle Phases
- Android官方Training阅读笔记 ---- Managing the Activity Lifecycle(Pausing and Resuming an Activity) (二)
- Android Develop Training——管理Activity的生命周期(Managing the Activity Lifecycle)
- android-Managing the Activity Lifecycle
- Android官方Training阅读笔记 ---- Managing the Activity Lifecycle(Stopping and Restarting an Activity) (三)
- Android Activity/Fragment Lifecycle
- Introduction to the Build Lifecycle
- Android开发 - ActivityLifecycleCallbacks使用方法初探
- Android Activity Lifecycle(Finish)
- The EC2 Instance Life Cycle
- #Maven# Introduction to the Build Lifecycle
- 转载:Understanding WPF Application Lifecycle
- Android 使用ActivityLifecycleCallbacks监听自己应用Activity的生命周期
- Understand the Lifecycle Callbacks