android - Find out whether the current activity will be task root eventually, after pending finishing activities have disappeared -
if firstactivity
root of task, , finishes , launches secondactivity
, calling istaskroot()
in secondactivity
return false
, because firstactivity
's finishing happens asynchronously , isn't done yet. waiting second , then calling istaskroot()
returns true.
public class firstactivity extends activity { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); finish(); startactivity(new intent(this, secondactivity.class)); } }
public class secondactivity extends activity { public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); } @override protected void onresume() { super.onresume(); ((textview)findviewbyid(r.id.tv1)) .settext("istaskroot() in onresume(): " + istaskroot()); new handler().postdelayed(new runnable() { @override public void run() { ((textview)findviewbyid(r.id.tv2)) .settext("istaskroot() after 1s: " + istaskroot()); } }, 1000); } }
is there way …
(optimally) find out whether activity task root eventually, or,
(better nothing) sort of notification/callback once task in "final" state ,
istaskroot()
return "truth"?
i've had similar problem , wanted tight control on root activity is. in case, root 1 of own activities (not 3rd party ones), able use following approach:
i extended application
class, added weak reference activity called currentrootactivity
, added synchronized getter , setter.
then managed state myself when activities created / destroyed. use case little special because looking replace 1 root another, knew reset new state variable, i'm pretty sure can same.
i able add state logic in shared base class of activities. wasn't disgusting sounds :)
as mentioned in comments, activity method isfinishing
might come in handy.
Comments
Post a Comment