当前位置:首页 > Android

提高Android Support Library 的稳定性

jsc10年前 (2016-04-07)Android3456

Crashlytics最近分析了近1亿个Android 应用Crash日志,发现有4%的Crash和 Support Library有关。经过进一步的分析发现这4%的Crash经常都是有几种同样的方式引起的。下面Crashlytics总结了使用Support Library的最佳实践:


1.AsyncTasks and Configuration Changes

AsyncTasks用来在后台执行长时间的操作,执行完成后更新UI界面。使用AsyncTasks并同时处理  configuration changes 是经常引起Bug的一种场景。当AsyncTask执行的时候,如果Fragment从Activity detach后 你再尝试获取activity,这是您的应用就有可能出现Crash,异常堆信息类似如下所示:

java.lang.IllegalStateException: Fragment MyFragment not attached to Activity
 at android.support.v4.app.Fragment.getResources(Fragment.java:551)
 at android.support.v4.app.Fragment.getString(Fragment.java:573)

在上面的堆栈信息中,Fragment依赖一个有效的activity来获取系统资源。一种避免该问题的方式是:在configuration changes 之间保持(retain)该AsyncTask。详情参考FragmentRetainInstance.java示例,该示例中使用RetainedFragment来执行后台操作。


2.安全的处理Fragment事务(Safely Performing Fragment Transactions)

Fragment事务用来添加、删除、或者替换Activity中的一个fragment。大多数的fragment事务都是在Activity的 onCreate函数或者用户交互中完成的。然后,我们也发现了一些情况是在Activity resume的时候来提交fragment事务的。这种情况可能会出现如下Crash:

java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
 at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1327)
 at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager:1338)
 at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
 at android.support.v4.app.BackStackRecord.commit(BackStackRecord:574)
 at android.support.v4.app.DialogFragment.show(DialogFragment:127)

只要FragmentActivity位于后台了(被其他应用挡住了,该应用暂停了), FragmentManagerImpl’smStateSaved 就会标记为true。该标记用来检测是否有状态丢失。当该标记为true的时候去提交一个事务,上面的IllegalStateException 异常就会发生。为了阻止状态丢失,在onSaveInstanceState()调用后,无法提交fragment事务。该Crash发生的场景是:当Activity 恢复的时候,在该标记还没设置为false之前onResume()就被调用了。

要防止该类型的Crash,只需要避免在onResume()函数中提交fragment事务即可。可以使用onResumeFragments()函数来处理Fragment状态,记得调用super.onResumeFragments().


3.管理Cursor的生命周期 (Managing the Cursor Lifecycle)

CursorAdapter方面在ListView中显示Cursor中的数据。但是,当cursor 无效的时候程序还尝试更新UI则会发现类似如下的异常:

java.lang.IllegalStateException: this should only be called when the cursor is valid
 at android.support.v4.widget.CursorAdapter.getView(CursorAdapter.java:245)
 at android.widget.HeaderViewListAdapter.getView(HeaderViewListAdapter.java:253)

当 CursorAdapter的 mDataValid 值为false的时候会发生该问题。有如下3种情况:

-  cursor 为null

-  重新执行查询,但是查询失败了

- 数据对象的onInvalidated() 函数被调用了

一种出现该问题的场景是:您同时使用CursorLoader和 startManagingCursor()来管理您的 Cursor。android开发团队建议用CursorLoader来替代 startManagingCursor()。如果您同时使用 Fragment,请务必使用CursorLoader来管理您的Cursor,不要再使用startManagingCursor()了。

结论

使用上面3种Support Library的最佳实践,在Support Library导致应用Crash的问题几乎不存在了。应用不崩溃了,客户满意了;客户满意了,五星就多了;五星多了,收入就多了!

尝试下Crashlytics for Android 来分析您的应用Crash日志并提交应用的稳定性吧!


转载来源: http://blog.chengyunfeng.com/?p=522#ixzz2dPkx1MsO
 


扫描二维码推送至手机访问。

版权声明:本文由微小站发布,如需转载请注明出处。

本文链接:https://www.jsc0.com/post/91.html

分享给朋友:

“提高Android Support Library 的稳定性 ” 的相关文章

Android基本功:支持GPS的核心API

一、LocationManager类 作用和TelephonyManager,AudioManager等服务类的作用类似,所有GPS定位相关的服务、对象都由该对象产生;       通过调用Context.getSystemService()方法获取实例对象......…

Android应用加入微信分享

Android应用加入微信分享

一、申请你的AppIDhttp://open.weixin.qq.com/  友情提示:推荐使用eclipse打包软件最后一步的MD5值去申请AppID二、官网下载libammsdk.jar包http://open.weixin.qq.com/download/?lang=zh_…

onTextChanged参数解释及实现EditText字数监听

由于最近做项目要检测EditText中输入的字数长度,从而接触到了Android中EditText的监听接口,TextWatcher。它有三个成员方法,第一个after很简单,这个方法就是在EditText内容已经改变之后调用,重点看下面两个方法:beforeTextChanged(CharSequ…

Activity的四种launchMode

Activity的四种launchMode

launchMode在多个Activity跳转的过程中扮演着重要的角色,它可以决定是否生成新的Activity实例,是否重用已存在的 Activity实例,是否和其他Activity实例公用一个task里。这里简单介绍一下task的概念,task是一个具有栈结构的对象,一个 task可以管理多个…

Activity之间数据交流(onActivityResult的用法)

Activity之间数据交流(onActivityResult的用法)

主要功能: 在一个主界面(主Activity)上能连接往许多不同子功能模块(子Activity上去),当子模块的事情做完之后就回到主界面,或许还同时返回一些子模块完成的数据交给主Activity处理。这样的数据交流就要用到回调函数onActivityResult。…