2011年3月7日 星期一

[Android] 如何不重複啟動相同的Activity

繼先前的文章-

[Android] Activity執行(啟動)另一個Activity 之 life cycle分析




如果你不想產生重複的Activity
請使用將intent的flag設定成FLAG_ACTIVITY_REORDER_TO_FRONT



01-01 00:47:25.641: VERBOSE/1(11937): onCreate
01-01 00:47:25.641: VERBOSE/1(11937): onStart
01-01 00:47:25.641: VERBOSE/1(11937): onResume
01-01 00:47:25.771: INFO/ActivityManager(2155): Displayed activity application.nelson/.CheckAndroidLifeCycle: 4075 ms (total 4075 ms)
01-01 00:47:30.811: DEBUG/dalvikvm(11709): GC_EXPLICIT freed 137 objects / 10120 bytes in 31ms
01-01 00:47:35.811: DEBUG/dalvikvm(11723): GC_EXPLICIT freed 882 objects / 59784 bytes in 33ms
01-01 00:47:40.811: DEBUG/dalvikvm(2295): GC_EXPLICIT freed 657 objects / 37136 bytes in 36ms
01-01 00:48:03.782: INFO/ActivityManager(2155): Starting activity: Intent { flg=0x20000 cmp=application.nelson/.OtherActivity }
01-01 00:48:03.791: VERBOSE/1(11937): onPause
01-01 00:48:03.871: WARN/Resources(11937): Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f050000}
01-01 00:48:03.871: VERBOSE/2(11937): onCreate
01-01 00:48:03.871: VERBOSE/2(11937): onStart
01-01 00:48:03.871: VERBOSE/2(11937): onResume
01-01 00:48:04.151: INFO/ActivityManager(2155): Displayed activity application.nelson/.OtherActivity: 350 ms (total 350 ms)
01-01 00:48:04.151: VERBOSE/1(11937): onStop
01-01 00:48:10.441: INFO/ActivityManager(2155): Starting activity: Intent { flg=0x20000 cmp=application.nelson/.CheckAndroidLifeCycle }
01-01 00:48:10.501: VERBOSE/2(11937): onPause
01-01 00:48:10.511: VERBOSE/1(11937): onStart
01-01 00:48:10.511: VERBOSE/1(11937): onResume
01-01 00:48:10.771: VERBOSE/2(11937): onStop


Source code download

Reference:
http://stackoverflow.com/questions/2232238/how-to-bring-an-activity-to-foreground-top-of-stack
http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_REORDER_TO_FRONT
http://developer.android.com/reference/android/content/Context.html#startActivity(android.content.Intent)

透過Facebook分享

沒有留言: