首页 > Android > 正文

提高Android Support Library 的稳定性

2016-04-07 Android 2536 ℃ 0 评论

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
 


猜你喜欢

日历
«    2024年3月    »
123
45678910
11121314151617
18192021222324
25262728293031
标签列表
最近发表
友情链接