Some interview topics related to Android Asynctask

Source: Internet
Author: User
Tags home screen

Asynctask after android4.0, Android uses a thread pool to implement, up to 5 threads, so a few asynctask execution time order is random,

If the 5 asynctask sequence executes, and there is another asynctask, then the last one will be suspended, and when 5 asynctask have one executed, the last one will be inserted and executed.

public class Testactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.test); new Fetchserverversionandsupportoshttptasker (). Execute (); new FetchServerVersionAndSupportOsHttpTasker1 (). Execute (); new FetchServerVersionAndSupportOsHttpTaske2 (). Execute ();        public class Fetchserverversionandsupportoshttptasker extends Asynctask<object, Integer, string> {@Override         Protected String doinbackground (Object ... params) {System.out.println ("=============first doinbackground");        Commonhttprequest.getforceupgraderelatedinfo ();        return "AAAA"; } @Override protected void OnPostExecute (String result) {if (!            Textutils.isempty (Result)) {System.out.println ("===============first postexecute"); }}} public class FetchServerVersionAndSupportOsHttpTasker1 extends Asynctask<object, Integer, string>        {@Override protected String doinbackground (Object ... params) {System.out.println ("=============second Doinba        Ckground ");        Commonhttprequest.getforceupgraderelatedinfo ();        Return "BBB"; } @Override protected void OnPostExecute (String result) {if (!            Textutils.isempty (Result)) {System.out.println ("===============second postexecute");         }}} public class FetchServerVersionAndSupportOsHttpTaske2 extends Asynctask<object, Integer, string> {  @Override protected String doinbackground (Object ... params) {System.out.println ("=============third        Doinbackground ");        Commonhttprequest.getforceupgraderelatedinfo ();        return "CCC"; } @Override protected void OnPostExecute (String result) {if (!            Textutils.isempty (Result)) {System.out.println ("===============third postexecute"); }        }    }}
Execution Result:
10-16 14:03:38.872:i/system.out (23132): =============third doinbackground10-16 14:03:38.872:i/system.out (23132): = = ===========second doinbackground10-16 14:03:38.872:i/system.out (23132): =============first doInBackground10-16 14:03:38.892:i/system.out (23132): ===============third postexecute10-16 14:03:38.922:i/system.out (23132): ======== =======second postexecute10-16 14:03:38.922:i/system.out (23132): ===============first PostExecute


Description of the stack:



TASK1 contains three activity: p,q,r; the boot sequence is P-->q-->rtask2 contains two activity: A, A, a, a, and a a-->b; in the boot order. TASK1 first starts p, then this p is the root activity for performing this task, and the task is defined under this activity. Defined by the taskaffinity in the Activity property in the properties manifest file.        (1) The p is pressed into the stack when it is started, because there is only one activity in the stack, so p is at the bottom of the stack and is at the top of the stack. At this point the user is able to see the P interface.        (2) p start Q, q into the stack. At this point the stack top is Q. Q renders the interface to cover p, p goes into the background (at this time the activity life cycle of the pause state) or into the background but does not run (this is the activity life cycle of the Stop state).        (3) q start R, r into the stack. At this point the stack top is R. The R-rendered interface will cover Q, and Q goes into the background to run.        (4) Pressing the back key is called the finish function to end R (activity) life. At this point R is out of the stack, and the life cycle of R ends.        (5) Continue back key, End Q's life, at this time Q out of the stack.        (6) Continue the back key, end the life of P, when p out of the stack.        (7) When all the activity in the stack has been stacked, the home screen interface will be displayed.

If A->b->c->c if C is singletop or Singletask then the last time C will not execute OnCreate
A if it is singletop A->b->c->a then a will be re-executed OnCreate, fallback to the CA if it is singletask, then a will not execute oncreate, fallback will return to home


Some interview topics related to Android Asynctask

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.