歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android Launcher 會onCreate 兩次的原因

Android Launcher 會onCreate 兩次的原因

日期:2017/3/1 10:41:04   编辑:Linux編程
  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. relaunchActivityLocked(r, r.configChangeFlags, true);
  36. r.configChangeFlags = 0;
  37. } else {
  38. if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
  39. "Switch is restarting non-resumed " + r);
  40. relaunchActivityLocked(r, r.configChangeFlags, false);
  41. r.configChangeFlags = 0;
  42. }
  43. // All done... tell the caller we weren't able to keep this
  44. // activity around.
  45. return false;
  46. }
  47. ...
  48. }
注意以上代碼中的第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">

Copyright © Linux教程網 All Rights Reserved