Activity 使用规范
Does not crash if the user receives a phone call or switches to another app while using your app.
Does not consume valuable system resources when the user is not actively using it.
Does not lose the user's progress if they leave your app and return to it at a later time.
Does not crash or lose the user's progress when the screen rotates between landscape and portrait orientation.
2.sdk 版本检查
用于过滤 版本兼容
Using the to prevent older systems from executing new APIs works in this way on Android 2.0 (API level 5) and higher only. Older versions will encounter a runtime exception.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { // For the main activity, make sure the app icon in the action bar // does not behave as a button ActionBar actionBar = getActionBar(); actionBar.setHomeButtonEnabled(false); }
3 onCreate() ,onStart() ,onResume() 这三个方法是一起连续执行的
Once the finishes execution, the system calls the
and
methods in quick succession. Your activity never resides in the Created or Started states. Technically, the activity becomes visible to the user when
is called, but
quickly follows and the activity remains in the Resumed state until something occurs to change that, such as when a phone call is received, the user navigates to another activity, or the device screen turns off.
4 onPause()
该方法可以实现1 规范里提出的一些问题,但该方法不能实现资源消耗太多的方法,会影响系统的流畅性,卡住,不容易切换到另一个Activity
Generally, you should not use to store user changes (such as personal information entered into a form) to permanent storage. The only time you should persist user changes to permanent storage within
is when you're certain users expect the changes to be auto-saved (such as when drafting an email). However, you should avoid performing CPU-intensive work during
, such as writing to a database, because it can slow the visible transition to the next activity (you should instead perform heavy-load shutdown operations during
).
Stop animations or other ongoing actions that could consume CPU.
Commit unsaved changes, but only if users expect such changes to be permanently saved when they leave (such as a draft email).
Release system resources, such as broadcast receivers, handles to sensors (like GPS), or any resources that may affect battery life while your activity is paused and the user does not need them.
@Overridepublic void onPause() { super.onPause(); // Always call the superclass method first // Release the Camera because we don't need it when paused // and other activities might need to use it. if (mCamera != null) { mCamera.release() mCamera = null; }}
5 onResume()
该方法是onPause()的一个互补方法,但是注意 当Activity 第一次构造时也会执行onResume(),所以代码里肯定有判断了。
When the user resumes your activity from the Paused state, the system calls the method.
Be aware that the system calls this method every time your activity comes into the foreground, including when it's created for the first time. As such, you should implement to initialize components that you release during
and perform any other initializations that must occur each time the activity enters the Resumed state (such as begin animations and initialize components only used while the activity has user focus)
@Overridepublic void onResume() { super.onResume(); // Always call the superclass method first // Get the Camera instance as the activity achieves full user focus if (mCamera == null) { initializeCamera(); // Local method to handle camera init }}
6.屏幕旋转时 Activity 会重建,那数据如何保存呢
As the system begins to stop your activity, it calls (1) so you can specify additional state data you'd like to save in case the
instance must be recreated. If the activity is destroyed and the same instance must be recreated, the system passes the state data defined at (1) to both the
method (2) and the
method (3).
static final String STATE_SCORE = "playerScore";static final String STATE_LEVEL = "playerLevel";...@Overridepublic void onSaveInstanceState(Bundle savedInstanceState) { // Save the user's current game state savedInstanceState.putInt(STATE_SCORE, mCurrentScore); savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel); // Always call the superclass so it can save the view hierarchy state super.onSaveInstanceState(savedInstanceState);}
@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Always call the superclass first // Check whether we're recreating a previously destroyed instance if (savedInstanceState != null) { // Restore value of members from saved state mCurrentScore = savedInstanceState.getInt(STATE_SCORE); mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL); } else { // Probably initialize members with default values for a new instance } ...}
public void onRestoreInstanceState(Bundle savedInstanceState) { // Always call the superclass so it can restore the view hierarchy super.onRestoreInstanceState(savedInstanceState); // Restore state members from saved instance mCurrentScore = savedInstanceState.getInt(STATE_SCORE); mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);}