zoukankan      html  css  js  c++  java
  • A journey on the Android Main Thread

    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

    引用地址:

  • 相关阅读:
    主流数据可视化工具介绍:帆软FineBI
    AngularJS之表格序号
    AngularJS之表格设置样式
    正则点号和问号 +
    中国市场主流商业智能工具的对比
    ssh 认证指定端口
    EL表达式中获取list长度
    java 如何将byte中的有效长度转换为String
    RedHat Enterprise Linux 6.4-x86_64 md5:467B53791903F9A0C477CBB1B24FFD1F
    14.5.5.3 How to Minimize and Handle Deadlocks 如何减少和处理死锁
  • 原文地址:https://www.cnblogs.com/freenovo/p/4469785.html
Copyright © 2011-2022 走看看