java - Activity can't be resumed -
over past months have been developing versatile real-time game engine, , have learned lot, still feel naive when comes application life cycle. specifically, trying implement activity can shuffled in background user, resumed.
my current architecture such: have xml menu launcher activity can create real-time game activity using intent. relevant data in game activity referenced through static data structures , variables. game activity creates worker threads in onsurfacecreate() callback of surfaceview object.
when user presses button, activity destroyed, , sent xml menu in launcher activity. fine, now. when user presses home button, activity sent background. ok, great. problems arise when user tries find way in game activity once has been sent background. when user touches launcher icon, game destroyed , menu re-launched. also, when user resumes game through task manager, onsurfacecreate() callback fires , worker threads started, game frozen.
so, have 2 questions. first, how resume activity through launcher icon, instead of re-launching game? second, when resume activity, actions necessary restart worker threads while persisting game data?
thanks!
edit: request, including code below. onsurfacecreate little bit complicated, sends message thread, implements callback. have verified implementation function fires when game activity resumed.
protected void surfacecreate() { log.e(tag, "surfacecreate"); thread gamethread = creategamethread(); gamethread.start(); } protected final void onresume() { super.onresume(); resume(); } protected final void onpause() { super.onpause(); pause(); }
these cryptic pause() , resume() methods set boolean variable prevents game logic being executed, nothing hinder worker threads, should continue looping.
edit: mohammad, solving first (although smaller) problem. turns out launcher icon behaves differently when not connected usb ide. second problem remains unresolved.
edit: working! second problem turned out issue unique code, apologize that. hope question can still useful dealing launcher , ides.
there multiple possible problems/solutions listed in post. includes misunderstanding of activity lifecycle , launchmode settings.
first, how resume activity through launcher icon, instead of re-launching game?
you missing definition onresume() , onpause() methods. examples here:
http://developer.android.com/training/basics/activity-lifecycle/pausing.html
straight api:
onpause() called part of activity lifecycle when activity going background, has not (yet) been killed.
onresume() called after onrestoreinstancestate(bundle), onrestart(), or onpause(), activity start interacting user.
check android activity lifecycle out:
http://developer.android.com/reference/android/app/activity.html
one theory on issue may you're going onstop() (by hitting home button) , android os looking onrestart(), can't find hence freezing/restarting.
second, when resume activity, actions necessary restart worker threads while persisting game data?
careful how use word restart. want pause , resume application (not restart). save data in onpause() (use database or other save feature you'd like). load data in onresume(). although, activity should resume normal if fill these methods.
now, if want save state when restart application, should save states in onstop() and/or ondestroy(). should load states in onstart(). in order save states, can check out:
https://stackoverflow.com/a/151940/2498729
for using eclipse (or other ide run/test application):
from have described have overridden android:launchmode in androidmanifest.xml or if testing "run as" eclipse try exiting application after installing , auto-starting. start again emulator , test home button behavior. suppose because android not put activities on os stack when started eclipse , home button behavior not usual. if not solve problem, try reading http://developer.android.com/guide/topics/fundamentals.html#lmodes.
i had launchmode set in startupactivity. removed (it set "singletask", behaves want it; app "resumed" activity expect, ie not startupactivity mainactivity.
source:
https://stackoverflow.com/a/1619461/2498729
http://developer.android.com/guide/topics/manifest/activity-element.html
according this:
https://stackoverflow.com/a/3002890/2498729
you should change andoird:launchmode "standard" (or "default").
standard: > b > home > b (what want) singletask: > b > home > (what don't want)
Comments
Post a Comment