博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ANDROID 学习笔记 一
阅读量:6412 次
发布时间:2019-06-23

本文共 5213 字,大约阅读时间需要 17 分钟。

  hot3.png

  1. 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);}

转载于:https://my.oschina.net/tacg/blog/343090

你可能感兴趣的文章
cd in bash
查看>>
rspamd 动态 add_header
查看>>
降低Redis内存占用
查看>>
Docker仓库Harbor配置LDAP并开启TLS认证
查看>>
NTLDR is missing
查看>>
通用权限管理系统组件 (GPM - General Permissions Manager) 中实现岗位的维护
查看>>
python内置函数与使用
查看>>
Linux route指定静态路由配置
查看>>
java操作redis
查看>>
一个简单的Dockerfile
查看>>
ajax原理
查看>>
Lambda表达式
查看>>
读懂Netty服务端开发(附学习代码)
查看>>
送给即将踏入软考征途的你
查看>>
这个图片功能咋生成的?
查看>>
使用jQuery和CSS创建一个粘性标题栏
查看>>
要命啦!Word中快速录入大全,内含快捷键小技巧,快来一起学习!
查看>>
智能计算,“芯”时代的华为云
查看>>
VB相册现在在
查看>>
C语言:流与缓冲区详解
查看>>