android singleTask / singleInstance问题

时间:2011-12-28 15:41:49

标签: android launchmode

我正在为Android设计一款游戏,我只想拥有它的一个实例。

游戏有一个主要活动,我称之为GameActivity,另一个活动叫做CreateAccountActivity。

我在清单中定义了GameActivity为android:launchMode="singleTask"

当应用程序第一次启动时,我使用startActivityForResult方法启动CreateAccoutActivity。

问题是,当我点击主页按钮然后启动我的应用程序时,没有任何反应(从用户的角度来看,但我从日志中看到CreateAccountActivity被销毁,GameActivity onActivityResult被调用然后它被销毁它也会使RESULT_CANCEL偏离正轨。

这是日志:

12-28 14:30:11.910: V/WindowManager(24745): Dsptch > Window{47ce9868 com.sec.android.app.twlauncher/com.sec.android.app.twlauncher.Launcher paused=false}
12-28 14:30:11.910: V/InputDevice(24745): ID[0]=0(0) Dn(0=>1)
12-28 14:30:11.973: D/dalvikvm(7770): GC_EXTERNAL_ALLOC freed 313 objects / 12648 bytes in 25ms
12-28 14:30:12.004: V/InputDevice(24745): ID[0]=0(0) Up(1=>0)
12-28 14:30:12.039: D/dalvikvm(7770): GC_EXTERNAL_ALLOC freed 88 objects / 2680 bytes in 25ms
12-28 14:30:12.110: D/dalvikvm(7770): GC_EXTERNAL_ALLOC freed 24 objects / 864 bytes in 25ms
12-28 14:30:12.157: V/WindowManager(24745): Dsptch > Window{47ce9868 com.sec.android.app.twlauncher/com.sec.android.app.twlauncher.Launcher paused=false}
12-28 14:30:12.157: I/ActivityManager(24745): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.*****.tq/.GameActivity }
12-28 14:30:12.161: D/Launcher.Workspace(7770): WorkspaceDraw: Workspace drawWallpaperImage()
12-28 14:30:12.168: I/CreateAccountActivity(9369): onDestroy
12-28 14:30:12.172: I/Launcher(7770): onPause()
12-28 14:30:12.184: W/Launcher(7770): WallpaperManager setVisibility visible true
12-28 14:30:12.203: I/GameActivity(9369): Game activity onActivityResult
12-28 14:30:12.203: I/GameActivity(9369): disconnecting from main server
12-28 14:30:12.203: I/ConnectionManager(9369): Disconnecting...
12-28 14:30:12.207: I/GameActivity(9369): Game activity onNewIntent is called
12-28 14:30:12.215: I/Launcher(7770): onWindowFocusChanged(false)
12-28 14:30:12.219: I/Launcher(7770): onResume(). mIsNewIntent : false
12-28 14:30:12.219: E/Launcher(7770): MTP-LAUNCHER: media scanning not yet finished . 
12-28 14:30:12.219: I/Launcher(7770): onResume() ended
12-28 14:30:12.219: I/Launcher(7770): onWindowFocusChanged(true)
12-28 14:30:12.223: W/InputManagerService(24745): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@47d9fa18
12-28 14:30:12.227: D/Launcher.Workspace(7770): WorkspaceDraw: Workspace drawWallpaperImage()
12-28 14:30:12.235: I/GameActivity(9369): Game activity destroyed
12-28 14:30:12.762: D/BatteryService(24745): update start
12-28 14:30:12.770: D/BatteryService(24745): updateBattery level:100 scale:100 status:5 health:2 present:true voltage: 4240 temperature: 360 technology: Li-ion AC powered:false USB powered:true icon:17302163
12-28 14:30:12.801: I/StatusBarPolicy(24745): BAT. status:5 health:2
12-28 14:30:12.801: D/WifiService(24745): ACTION_BATTERY_CHANGED pluggedType: 2
12-28 14:30:13.231: W/Launcher(7770): WallpaperManager setVisibility visible false
12-28 14:30:13.375: D/Launcher.Workspace(7770): WorkspaceDraw: Workspace drawWallpaperImage()

为简单起见我这样描述: 活动A = MAIN - singleTask 活动B =(未定义 - 标准)

A startsForResult B - >主页按钮 - >游戏启动按钮 - > B被破坏 - > onActivityResult被称为 - >被毁 - >游戏启动按钮 - >一个startstForResult B ...

修改

我现在怀疑是否需要使用singleTask。

是真的,无论我从哪里开始我的活动,而不是我编写的意图,android会一直重启我的应用程序,我的活动不会有任何多个实例?

如果是这样的话,那就是goo -ood:)

1 个答案:

答案 0 :(得分:0)

来自活动文档

  

“singleTask”活动可能有也可能没有其他活动   在堆栈中。如果确实如此,则无法处理意图,   意图被删除了。 (即使意图被删除,它的   到来会导致任务到达前台,在那里   会留下来。)

点击游戏启动时,创建此Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.*****.tq/.GameActivity GameActivity(A)试图处理onNewIntent()中的意图。您可以放置​​一个Log.d(“GameActivity”,“onNewIntent”)来验证这一点。 (B)在设定结果之前被简单地销毁。

也许你可以在CreateAccountActivity.onDestroy()中设置setResult(),以便在GameActivity中处理它?<​​/ p>