我正在设计一款android游戏,我希望它只有一个实例。
游戏有主活动,我叫它GameActivity和另一个活动,我叫它CreateAccountActivity。
我在manifest中定义GameActivity为android:launchMode="singleTask"
。
当应用程序第一次启动时,我使用startActivityForResult
方法启动CreateAccoutActivity
问题是,当我按下home键然后启动我的应用程序时,什么都没有发生(从用户的角度来看,但我从日志中看到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 startstForResult B -> home键->游戏启动键-> B destroyed -> A onActivityResult被调用-> A destroyed ->游戏启动键-> A startstForResult B…
编辑
我现在怀疑我是否需要使用singleTask。
是真的,无论我从哪里开始我的活动由android,而不是由一个意图我编码,android将始终重新启动我的应用程序,不会有我的活动的任何多个实例吗?
如果是这样的话,任何东西都是好的:)
从活动文档
一个"singleTask"活动可以有也可以没有其他活动在它上面在堆栈中。如果有,它就不在处理意图的位置上,意图被丢弃了。(即使意图被删除,它到达会使任务来到前台,在那里它将保持。)
当你点击游戏启动时,创建这个Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.*****.tq/.GameActivity
GameActivity(A)试图处理onNewIntent()中的意图。你可以放一个圆木。d("GameActivity","onNewIntent is called")来验证这一点。(B)在可以设置结果之前被简单地销毁。
也许你可以在createaccountacactivity . ondestroy()中设置result(),这样你就可以在GameActivity中处理它了?