Activity switch and Async Android -
i have 2 activities: mainactivity
associated main.xml
layout (app "home" screen) , aboutactivity
associated about.xml
layout (app "about" screen).
while in aboutactivity
, async
task within mainactivity
still tries access main.xml
. app stop working result.
is there way can?
- "pause"
async
task withinmainactivity
, "resume" them when user switchesaboutactivity
- or still access
main.xml
in background while inaboutactivity
additional information:
mainactivity
launch activity. aboutactivity extends mainactivity
. users can go "about" screen/ switch aboutactivity
using option menu.
the async
task within mainactivity
put user's current location textview. about.xml
contains static text. aboutactivity
nothing show about.xml
.
aboutactivity:
public class aboutactivity extends mainactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.about); } }
mainactivity:
public class mainactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); // creating new non-ui thread task download google place json data placestask placestask = new placestask(); // invokes "doinbackground()" method of class placetask placestask.execute(sb.tostring()); } /** class, download google places */ private class placestask extends asynctask<string, integer, string>{ string data = null; // invoked execute() method of object @override protected string doinbackground(string... url) { //make asynctask wait debugger //android.os.debug.waitfordebugger(); try{ data = downloadurl(url[0]); }catch(exception e){ log.d(debug,e.tostring()); } return data; } // executed after complete execution of doinbackground() method @override protected void onpostexecute(string result){ textview curloc = (textview) findviewbyid(r.id.currentlocation); curloc.settext(result); } } }
asynctask not pause can store data temporary when task complete , while start background activity show data on textview.
Comments
Post a Comment