1page.title=Android Application Framework FAQ 2excludeFromSuggestions=true 3@jd:body 4 5<ul> 6 <li><a href="#1">Do all the Activities and Services of an 7 application run in a single process?</a></li> 8 <li><a href="#2">Do all Activities run in the main thread of 9 an application process?</a></li> 10 <li><a href="#3">How do I pass complicated data structures 11 from one Activity/Service to another?</a></li> 12 <li><a href="#4">How can I check if an Activity is already 13 running before starting it?</a></li> 14 <li><a href="#5">If an Activity starts a remote service, is 15 there any way for the Service to pass a message back to the Activity?</a></li> 16 <li><a href="#6">How to avoid getting the Application not 17 responding dialog?</a></li> 18 <li><a href="#7">How does an application know if a package is 19 added or removed?</a></li> 20</ul> 21 22 23<a name="1" id="1"></a> 24 25<h2>Do all the Activities and Services of an application run in a 26single process?</h2> 27 28<p>All Activities and Services in an application run in a single process by 29default. If needed, you can declare an <code>android:process</code> attribute 30in your manifest file, to explicitly place a component (Activity/Service) in 31another process.</p> 32 33 34 35<a name="2" id="2"></a> 36 37<h2>Do all Activities run in the main thread of an application 38process?</h2> 39 40<p>By default, all of the application code in a single process runs 41in the main UI thread. This is the same thread 42that also handles UI events. The only exception is the code that handles 43IPC calls coming in from other processes. The system maintains a 44separate pool of transaction threads in each process to dispatch all 45incoming IPC calls. The developer should create separate threads for any 46long-running code, to avoid blocking the main UI thread.</p> 47 48 49 50<a name="3" id="3"></a> 51 52<h2>How do I pass data between Activities/Services within a single 53application?</h2> 54 55<p>It depends on the type of data that you want to share:</p> 56 57<h3>Primitive Data Types</h3> 58 59<p>To share primitive data between Activities/Services in an 60application, use Intent.putExtras(). For passing primitive data that 61needs to persist use the 62<a href="{@docRoot}guide/topics/data/data-storage.html#preferences"> 63Preferences</a> storage mechanism.</p> 64 65<h3>Non-Persistent Objects</h3> 66 67<p>For sharing complex non-persistent user-defined objects for short 68duration, the following approaches are recommended: 69</p> 70 <h4>Singleton class</h4> 71 <p>You can take advantage of the fact that your application 72components run in the same process through the use of a singleton. 73This is a class that is designed to have only one instance. It 74has a static method with a name such as <code>getInstance()</code> 75that returns the instance; the first time this method is called, 76it creates the global instance. Because all callers get the same 77instance, they can use this as a point of interaction. For 78example activity A may retrieve the instance and call setValue(3); 79later activity B may retrieve the instance and call getValue() to 80retrieve the last set value.</p> 81 82 <h4>A public static field/method</h4> 83 <p>An alternate way to make data accessible across Activities/Services is to use <em>public static</em> 84fields and/or methods. You can access these static fields from any other 85class in your application. To share an object, the activity which creates your object sets a 86static field to point to this object and any other activity that wants to use 87this object just accesses this static field.</p> 88 89 <h4>A HashMap of WeakReferences to Objects</h4> 90 <p>You can also use a HashMap of WeakReferences to Objects with Long 91keys. When an activity wants to pass an object to another activity, it 92simply puts the object in the map and sends the key (which is a unique 93Long based on a counter or time stamp) to the recipient activity via 94intent extras. The recipient activity retrieves the object using this 95key.</p> 96 97<h3>Persistent Objects</h3> 98 99<p>Even while an application appears to continue running, the system 100may choose to kill its process and restart it later. If you have data 101that you need to persist from one activity invocation to the next, you 102need to represent that data as state that gets saved by an activity when 103it is informed that it might go away.</p> 104 105<p>For sharing complex persistent user-defined objects, the 106following approaches are recommended: 107<ul> 108 <li>Application Preferences</li> 109 <li>Files</li> 110 <li>contentProviders</li> 111 <li>SQLite DB</li> 112</ul> 113</p> 114 115<p>If the shared data needs to be retained across points where the application 116process can be killed, then place that data in persistent storage like 117Application Preferences, SQLite DB, Files or ContentProviders. Please refer to 118the <a href="{@docRoot}guide/topics/data/data-storage.html">Data Storage</a> 119for further details on how to use these components.</p> 120 121 122 123 124<a name="4" id="4"></a> 125 126<h2>How can I check if an Activity is already running before starting 127it?</h2> 128 129<p>The general mechanism to start a new activity if its not running— 130or to bring the activity stack to the front if is already running in the 131background— is the to use the NEW_TASK_LAUNCH flag in the startActivity() 132call.</p> 133 134 135 136<a name="5" id="5"></a> 137 138<h2>If an Activity starts a remote service, is there any way for the 139Service to pass a message back to the Activity?</h2> 140 141<p>See the {@link android.app.Service} documentation's for examples of 142how clients can interact with a service. You can take advantage of the 143fact that your components run in the same process to greatly simplify 144service interaction from the generic remote case, as shown by the "Local 145Service Sample". In some cases techniques like singletons may also make sense. 146 147 148<a name="6" id="6"></a> 149 150<h2>How to avoid getting the Application not responding dialog?</h2> 151 152<p>Please read the <a href="{@docRoot}guide/practices/design/responsiveness.html">Designing for Responsiveness</a> 153document.</p> 154 155 156 157 158<a name="7" id="7"></a> 159 160<h2>How does an application know if a package is added or removed? 161</h2> 162 163<p>Whenever a package is added, an intent with PACKAGE_ADDED action 164is broadcast by the system. Similarly when a package is removed, an 165intent with PACKAGE_REMOVED action is broadcast. To receive these 166intents, you should write something like this: 167<pre> 168 <receiver android:name ="com.android.samples.app.PackageReceiver"> 169 <intent-filter> 170 <action android:name="android.intent.action.PACKAGE_ADDED"/> 171 <action android:name="android.intent.action.PACKAGE_REMOVED"/> 172 173 <data android:scheme="package" /> 174 </intent-filter> 175 </receiver> 176 </pre> 177 <br> 178Here PackageReceiver is a BroadcastReceiver class.Its onReceive() 179method is invoked, every time an application package is installed or 180removed. 181 182</p> 183 184 185 186