Android Launcher 会onCreate 两次的原因


  1. com.Android.server.am.ActivityStack  
  2.    /** 
  3.     * Make sure the given activity matches the current configuration.  Returns 
  4.     * false if the activity had to be destroyed.  Returns true if the 
  5.     * configuration is the same, or the activity will remain running as-is 
  6.     * for whatever reason.  Ensures the HistoryRecord is updated with the 
  7.     * correct configuration and all other bookkeeping is handled. 
  8.     */  
  9.    final boolean ensureActivityConfigurationLocked(ActivityRecord r,  
  10.            int globalChanges) {  
  11.        ...  
  12.        if ((changes&(~r.info.configChanges)) != 0) {  
  13.            // Aha, the activity isn't handling the change, so DIE DIE DIE.   
  14.            r.configChangeFlags |= changes;  
  15.            r.startFreezingScreenLocked(r.app, globalChanges);  
  16.            if (r.app == null || r.app.thread == null) {  
  17.                if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,  
  18.                        "Switch is destroying non-running " + r);  
  19.                destroyActivityLocked(r, true);  
  20.            } else if (r.state == ActivityState.PAUSING) {  
  21.                // A little annoying: we are waiting for this activity to   
  22.                // finish pausing.  Let's not do anything now, but just   
  23.                // flag that it needs to be restarted when done pausing.   
  24.                if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,  
  25.                        "Switch is skipping already pausing " + r);  
  26.                r.configDestroy = true;  
  27.                return true;  
  28.            } else if (r.state == ActivityState.RESUMED) {  
  29.                // Try to optimize this case: the configuration is changing   
  30.                // and we need to restart the top, resumed activity.   
  31.                // Instead of doing the normal handshaking, just say   
  32.                // "restart!".   
  33.                if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,  
  34.                        "Switch is restarting resumed " + r);  
  35.                  
  36.                relaunchActivityLocked(r, r.configChangeFlags, true);  
  37.                r.configChangeFlags = 0;  
  38.            } else {  
  39.                if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,  
  40.                        "Switch is restarting non-resumed " + r);  
  41.                relaunchActivityLocked(r, r.configChangeFlags, false);  
  42.                r.configChangeFlags = 0;  
  43.            }  
  44.              
  45.            // All done...  tell the caller we weren't able to keep this   
  46.            // activity around.   
  47.            return false;  
  48.        }  
  49.        ...  
  50.       }  
注意以上代码中的第13行和第41行

Why:

        系统启动后,搜索到可用移动网络会触发 config changes : mcc | mnc 

        “mcc“   The IMSI mobile country code (MCC) has changed — that is, a SIM hasbeen detected and updated the MCC.移动国家号码,由三位数字组成,每个国家都有自己独立的MCC,可以识别手机用户所属国家。
        “mnc“   The IMSI mobile network code (MNC) has changed — that is, a SIM hasbeen detected and updated the MNC.移动网号,在一个国家或者地区中,用于区分手机用户的服务商。

HowTo:

<activity android:name="Launcher" android:launchMode="singleTask" android:configChanges="mcc|mnc|keyboardHidden|orientation">

相关内容