Home | History | Annotate | Download | only in faq
      1 page.title=Android Application Framework FAQ
      2 parent.title=FAQs, Tips, and How-to
      3 parent.link=index.html
      4 @jd:body
      5 
      6 <ul>
      7   <li><a href="#1">Do all the Activities and Services of an
      8   application run in a single process?</a></li>
      9   <li><a href="#2">Do all Activities run in the main thread of
     10   an application process?</a></li>
     11   <li><a href="#3">How do I pass complicated data structures
     12   from one Activity/Service to another?</a></li>
     13   <li><a href="#4">How can I check if an Activity is already
     14   running before starting it?</a></li>
     15   <li><a href="#5">If an Activity starts a remote service, is
     16   there any way for the Service to pass a message back to the Activity?</a></li>
     17   <li><a href="#6">How to avoid getting the Application not
     18   responding dialog?</a></li>
     19   <li><a href="#7">How does an application know if a package is
     20   added or removed?</a></li>
     21 </ul>
     22 
     23 
     24 <a name="1" id="1"></a>
     25 
     26 <h2>Do all the Activities and Services of an application run in a
     27 single process?</h2>
     28 
     29 <p>All Activities and Services in an application run in a single process by
     30 default. If needed, you can declare an <code>android:process</code> attribute
     31 in your manifest file, to explicitly place a component (Activity/Service) in
     32 another process.</p>
     33 
     34 
     35 
     36 <a name="2" id="2"></a>
     37 
     38 <h2>Do all Activities run in the main thread of an application
     39 process?</h2>
     40 
     41 <p>By default, all of the application code in a single process runs
     42 in the main UI thread. This is the same thread
     43 that also handles UI events. The only exception is the code that handles
     44 IPC calls coming in from other processes. The system maintains a
     45 separate pool of transaction threads in each process to dispatch all
     46 incoming IPC calls. The developer should create separate threads for any
     47 long-running code, to avoid blocking the main UI thread.</p>
     48 
     49 
     50 
     51 <a name="3" id="3"></a>
     52 
     53 <h2>How do I pass data between Activities/Services within a single
     54 application?</h2>
     55 
     56 <p>It depends on the type of data that you want to share:</p>
     57 
     58 <h3>Primitive Data Types</h3>
     59 
     60 <p>To share primitive data between Activities/Services in an
     61 application, use Intent.putExtras(). For passing primitive data that
     62 needs to persist use the 
     63 <a href="{@docRoot}guide/topics/data/data-storage.html#preferences">
     64 Preferences</a> storage mechanism.</p>
     65 
     66 <h3>Non-Persistent Objects</h3>
     67 
     68 <p>For sharing complex non-persistent user-defined objects for short
     69 duration, the following approaches are recommended:
     70 </p>
     71   <h4>The android.app.Application class</h4>
     72   <p>The android.app.Application is a base class for those who need to
     73 maintain global application state. It can be accessed via
     74 getApplication() from any Activity or Service. It has a couple of
     75 life-cycle methods and will be instantiated by Android automatically if
     76 your register it in AndroidManifest.xml.</p>
     77 
     78   <h4>A public static field/method</h4>
     79   <p>An alternate way to make data accessible across Activities/Services is to use <em>public static</em>
     80 fields and/or methods. You can access these static fields from any other
     81 class in your application. To share an object, the activity which creates your object sets a
     82 static field to point to this object and any other activity that wants to use
     83 this object just accesses this static field.</p>
     84 
     85   <h4>A HashMap of WeakReferences to Objects</h4>
     86   <p>You can also use a HashMap of WeakReferences to Objects with Long
     87 keys. When an activity wants to pass an object to another activity, it
     88 simply puts the object in the map and sends the key (which is a unique
     89 Long based on a counter or time stamp) to the recipient activity via
     90 intent extras. The recipient activity retrieves the object using this
     91 key.</p>
     92 
     93   <h4>A Singleton class</h4>
     94   <p>There are advantages to using a static Singleton, such as you can
     95 refer to them without casting getApplication() to an
     96 application-specific class, or going to the trouble of hanging an
     97 interface on all your Application subclasses so that your various
     98 modules can refer to that interface instead. </p>
     99 <p>But, the life cycle of a static is not well under your control; so
    100 to abide by the life-cycle model, the application class should initiate and
    101 tear down these static objects in the onCreate() and onTerminate() methods
    102 of the Application Class</p>
    103 </p>
    104 
    105 <h3>Persistent Objects</h3>
    106 
    107 <p>Even while an application appears to continue running, the system
    108 may choose to kill its process and restart it later. If you have data
    109 that you need to persist from one activity invocation to the next, you
    110 need to represent that data as state that gets saved by an activity when
    111 it is informed that it might go away.</p>
    112 
    113 <p>For sharing complex persistent user-defined objects, the
    114 following approaches are recommended:
    115 <ul>
    116   <li>Application Preferences</li>
    117   <li>Files</li>
    118   <li>contentProviders</li>
    119   <li>SQLite DB</li>
    120 </ul>
    121 </p>
    122 
    123 <p>If the shared data needs to be retained across points where the application
    124 process can be killed, then place that data in persistent storage like
    125 Application Preferences, SQLite DB, Files or ContentProviders. Please refer to
    126 the <a href="{@docRoot}guide/topics/data/data-storage.html">Data Storage</a>
    127 for further details on how to use these components.</p>
    128 
    129 
    130 
    131 
    132 <a name="4" id="4"></a>
    133 
    134 <h2>How can I check if an Activity is already running before starting
    135 it?</h2>
    136 
    137 <p>The general mechanism to start a new activity if its not running&mdash;
    138 or to bring the activity stack to the front if is already running in the
    139 background&mdash; is the to use the NEW_TASK_LAUNCH flag in the startActivity()
    140 call.</p>
    141 
    142 
    143 
    144 <a name="5" id="5"></a>
    145 
    146 <h2>If an Activity starts a remote service, is there any way for the
    147 Service to pass a message back to the Activity?</h2>
    148 
    149 <p>The remote service can define a callback interface and register it with the
    150 clients to callback into the clients. The 
    151 {@link android.os.RemoteCallbackList RemoteCallbackList} class provides methods to
    152 register and unregister clients with the service, and send and receive
    153 messages.</p>
    154 
    155 <p>The sample code for remote service callbacks is given in <a
    156 href="{@docRoot}guide/samples/ApiDemos/src/com/example/android/apis/app/RemoteService.html">ApiDemos/RemoteService</a></p>
    157 
    158 
    159 
    160 <a name="6" id="6"></a>
    161 
    162 <h2>How to avoid getting the Application not responding dialog?</h2>
    163 
    164 <p>Please read the <a href="{@docRoot}guide/practices/design/responsiveness.html">Designing for Responsiveness</a> 
    165 document.</p>
    166 
    167 
    168 
    169 
    170 <a name="7" id="7"></a>
    171 
    172 <h2>How does an application know if a package is added or removed?
    173 </h2>
    174 
    175 <p>Whenever a package is added, an intent with PACKAGE_ADDED action
    176 is broadcast by the system. Similarly when a package is removed, an
    177 intent with PACKAGE_REMOVED action is broadcast. To receive these
    178 intents, you should write something like this:
    179 <pre>
    180        &lt;receiver android:name ="com.android.samples.app.PackageReceiver"&gt;
    181             &lt;intent-filter&gt;
    182              &lt;action android:name="android.intent.action.PACKAGE_ADDED"/&gt;
    183               &lt;action android:name="android.intent.action.PACKAGE_REMOVED"/&gt;            
    184             
    185               &lt;data android:scheme="package" /&gt;
    186             &lt;/intent-filter&gt;
    187         &lt;/receiver&gt;
    188   </pre>
    189   <br>
    190 Here PackageReceiver is a BroadcastReceiver class.Its onReceive()
    191 method is invoked, every time an application package is installed or
    192 removed.
    193 
    194 </p>
    195 
    196 
    197 
    198