A journey on the Android Main Thread - Lifecycle b
2014-03-03 17:56
351 查看
In the previous part we took a dive into loopers and handlers and how they relate to the Android main thread.
Today, we will take a closer look at how the main thread interacts with the Android components lifecycle.
Activities love orientation changes
Let's start with the activity lifecycle and the magic behind the handling of configuration changes.
Why it matters
This article was inspired by a real crash that occurred in Square Register.
A simplified version of the code is:
de >public class MyActivity extends Activity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Handler handler = new Handler(Looper.getMainLooper()); handler.post(new Runnable() { public void run() { doSomething(); } }); } void doSomething() { // Uses the activity instance }}de>
As we will see, de>doSomething()de> can be called after the activity de>onDestroy()de> method has been called due to a configuration change. At that point, you should not use the activity instance anymore.
A refresher on orientation changes
The device orientation can change at any time. We will simulate an orientation change while the activity is being created using Activity#setRequestedOrientation(int).
Can you predict the log output when starting this activity in portrait?
de >public class MyActivity extends Activity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d("Square", "onCreate()"); if (savedInstanceState == null) { Log.d("Square", "Requesting orientation change"); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } } protected void onResume() { super.onResume(); Log.d("Square", "onResume()"); } protected void onPause() { super.onPause(); Log.d("Square", "onPause()"); } protected void onDestroy() { super.onDestroy(); Log.d("Square", "onDestroy()"); }}de>
If you know the Android lifecycle, you probably predicted this:
de >onCreate()Requesting orientation changeonResume()onPause()onDestroy()onCreate()onResume()de>
The Android Lifecycle goes on normally, the activity is created, resumed, and then the orientation change is taken into account and the activity is paused, destroyed, and a new activity is created and resumed.
Orientation changes and the main thread
Here is an important detail to remember: an orientation change leads to recreating the activity via a simple post of a message to the main thread looper queue.
Let's look at that by writing a spy that will read the content of the looper queue via reflection:
de >public class MainLooperSpy { private final Field messagesField; private final Field nextField; private final MessageQueue mainMessageQueue; public MainLooperSpy() { try { Field queueField = Looper.class.getDeclaredField("mQueue"); queueField.setAccessible(true); messagesField = MessageQueue.class.de>
Tag:
Freenovo 发表于2014-03-03 17:56:00 | 编辑 | 分享 0
引用地址:
Today, we will take a closer look at how the main thread interacts with the Android components lifecycle.
Activities love orientation changes
Let's start with the activity lifecycle and the magic behind the handling of configuration changes.
Why it matters
This article was inspired by a real crash that occurred in Square Register.
A simplified version of the code is:
de >public class MyActivity extends Activity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Handler handler = new Handler(Looper.getMainLooper()); handler.post(new Runnable() { public void run() { doSomething(); } }); } void doSomething() { // Uses the activity instance }}de>
As we will see, de>doSomething()de> can be called after the activity de>onDestroy()de> method has been called due to a configuration change. At that point, you should not use the activity instance anymore.
A refresher on orientation changes
The device orientation can change at any time. We will simulate an orientation change while the activity is being created using Activity#setRequestedOrientation(int).
Can you predict the log output when starting this activity in portrait?
de >public class MyActivity extends Activity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d("Square", "onCreate()"); if (savedInstanceState == null) { Log.d("Square", "Requesting orientation change"); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } } protected void onResume() { super.onResume(); Log.d("Square", "onResume()"); } protected void onPause() { super.onPause(); Log.d("Square", "onPause()"); } protected void onDestroy() { super.onDestroy(); Log.d("Square", "onDestroy()"); }}de>
If you know the Android lifecycle, you probably predicted this:
de >onCreate()Requesting orientation changeonResume()onPause()onDestroy()onCreate()onResume()de>
The Android Lifecycle goes on normally, the activity is created, resumed, and then the orientation change is taken into account and the activity is paused, destroyed, and a new activity is created and resumed.
Orientation changes and the main thread
Here is an important detail to remember: an orientation change leads to recreating the activity via a simple post of a message to the main thread looper queue.
Let's look at that by writing a spy that will read the content of the looper queue via reflection:
de >public class MainLooperSpy { private final Field messagesField; private final Field nextField; private final MessageQueue mainMessageQueue; public MainLooperSpy() { try { Field queueField = Looper.class.getDeclaredField("mQueue"); queueField.setAccessible(true); messagesField = MessageQueue.class.de>
Tag:
Freenovo 发表于2014-03-03 17:56:00 | 编辑 | 分享 0
引用地址:
相关文章推荐
- 利用Android源代码编译一个自己写的Hello World模块
- Android如何防止apk程序被反编译
- Android之系统自带的文字外观设置及实际显示效果图
- Android APK反编译详解(附图)
- 使用Loaders
- 一个帖子学会Android开发四大组件 (详细篇)
- Android 手动显示和隐藏软键盘 (转)
- 【Android Developers Training】 74. 序言:通过无线连接设备
- android 性能优化总结
- Android四大基本组件介绍与生命周期
- android X86之旅
- Androidpn的使用
- 基于Qt-Windows平台的Android开发部署
- 解决android加载大图片,内存溢出问题
- android自动化测试之生成单元测试报告
- 遮罩层 CCClippingNode Stencil buffer is not enabled (ios/android)
- Android 中点击返回键弹出“在按一次退出程序”的做法
- Android软键盘在清单文件中所有属性解释大全
- [Android] android核心分析
- android基础1
MeasureSpec]
对网络上关于listview异步加载优化方案的文章的总结