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>Singleton class</h4>
     72   <p>You can take advantage of the fact that your application
     73 components run in the same process through the use of a singleton.
     74 This is a class that is designed to have only one instance.  It
     75 has a static method with a name such as <code>getInstance()</code>
     76 that returns the instance; the first time this method is called,
     77 it creates the global instance.  Because all callers get the same
     78 instance, they can use this as a point of interaction.  For
     79 example activity A may retrieve the instance and call setValue(3);
     80 later activity B may retrieve the instance and call getValue() to
     81 retrieve the last set value.</p>
     82 
     83   <h4>A public static field/method</h4>
     84   <p>An alternate way to make data accessible across Activities/Services is to use <em>public static</em>
     85 fields and/or methods. You can access these static fields from any other
     86 class in your application. To share an object, the activity which creates your object sets a
     87 static field to point to this object and any other activity that wants to use
     88 this object just accesses this static field.</p>
     89 
     90   <h4>A HashMap of WeakReferences to Objects</h4>
     91   <p>You can also use a HashMap of WeakReferences to Objects with Long
     92 keys. When an activity wants to pass an object to another activity, it
     93 simply puts the object in the map and sends the key (which is a unique
     94 Long based on a counter or time stamp) to the recipient activity via
     95 intent extras. The recipient activity retrieves the object using this
     96 key.</p>
     97 
     98 <h3>Persistent Objects</h3>
     99 
    100 <p>Even while an application appears to continue running, the system
    101 may choose to kill its process and restart it later. If you have data
    102 that you need to persist from one activity invocation to the next, you
    103 need to represent that data as state that gets saved by an activity when
    104 it is informed that it might go away.</p>
    105 
    106 <p>For sharing complex persistent user-defined objects, the
    107 following approaches are recommended:
    108 <ul>
    109   <li>Application Preferences</li>
    110   <li>Files</li>
    111   <li>contentProviders</li>
    112   <li>SQLite DB</li>
    113 </ul>
    114 </p>
    115 
    116 <p>If the shared data needs to be retained across points where the application
    117 process can be killed, then place that data in persistent storage like
    118 Application Preferences, SQLite DB, Files or ContentProviders. Please refer to
    119 the <a href="{@docRoot}guide/topics/data/data-storage.html">Data Storage</a>
    120 for further details on how to use these components.</p>
    121 
    122 
    123 
    124 
    125 <a name="4" id="4"></a>
    126 
    127 <h2>How can I check if an Activity is already running before starting
    128 it?</h2>
    129 
    130 <p>The general mechanism to start a new activity if its not running&mdash;
    131 or to bring the activity stack to the front if is already running in the
    132 background&mdash; is the to use the NEW_TASK_LAUNCH flag in the startActivity()
    133 call.</p>
    134 
    135 
    136 
    137 <a name="5" id="5"></a>
    138 
    139 <h2>If an Activity starts a remote service, is there any way for the
    140 Service to pass a message back to the Activity?</h2>
    141 
    142 <p>See the {@link android.app.Service} documentation's for examples of
    143 how clients can interact with a service.  You can take advantage of the
    144 fact that your components run in the same process to greatly simplify
    145 service interaction from the generic remote case, as shown by the "Local
    146 Service Sample".  In some cases techniques like singletons may also make sense.
    147 
    148 
    149 <a name="6" id="6"></a>
    150 
    151 <h2>How to avoid getting the Application not responding dialog?</h2>
    152 
    153 <p>Please read the <a href="{@docRoot}guide/practices/design/responsiveness.html">Designing for Responsiveness</a> 
    154 document.</p>
    155 
    156 
    157 
    158 
    159 <a name="7" id="7"></a>
    160 
    161 <h2>How does an application know if a package is added or removed?
    162 </h2>
    163 
    164 <p>Whenever a package is added, an intent with PACKAGE_ADDED action
    165 is broadcast by the system. Similarly when a package is removed, an
    166 intent with PACKAGE_REMOVED action is broadcast. To receive these
    167 intents, you should write something like this:
    168 <pre>
    169        &lt;receiver android:name ="com.android.samples.app.PackageReceiver"&gt;
    170             &lt;intent-filter&gt;
    171              &lt;action android:name="android.intent.action.PACKAGE_ADDED"/&gt;
    172               &lt;action android:name="android.intent.action.PACKAGE_REMOVED"/&gt;            
    173             
    174               &lt;data android:scheme="package" /&gt;
    175             &lt;/intent-filter&gt;
    176         &lt;/receiver&gt;
    177   </pre>
    178   <br>
    179 Here PackageReceiver is a BroadcastReceiver class.Its onReceive()
    180 method is invoked, every time an application package is installed or
    181 removed.
    182 
    183 </p>
    184 
    185 
    186 
    187