1page.title=Android Application Framework FAQ 2parent.title=FAQs, Tips, and How-to 3parent.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 27single process?</h2> 28 29<p>All Activities and Services in an application run in a single process by 30default. If needed, you can declare an <code>android:process</code> attribute 31in your manifest file, to explicitly place a component (Activity/Service) in 32another 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 39process?</h2> 40 41<p>By default, all of the application code in a single process runs 42in the main UI thread. This is the same thread 43that also handles UI events. The only exception is the code that handles 44IPC calls coming in from other processes. The system maintains a 45separate pool of transaction threads in each process to dispatch all 46incoming IPC calls. The developer should create separate threads for any 47long-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 54application?</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 61application, use Intent.putExtras(). For passing primitive data that 62needs to persist use the 63<a href="{@docRoot}guide/topics/data/data-storage.html#preferences"> 64Preferences</a> storage mechanism.</p> 65 66<h3>Non-Persistent Objects</h3> 67 68<p>For sharing complex non-persistent user-defined objects for short 69duration, the following approaches are recommended: 70</p> 71 <h4>Singleton class</h4> 72 <p>You can take advantage of the fact that your application 73components run in the same process through the use of a singleton. 74This is a class that is designed to have only one instance. It 75has a static method with a name such as <code>getInstance()</code> 76that returns the instance; the first time this method is called, 77it creates the global instance. Because all callers get the same 78instance, they can use this as a point of interaction. For 79example activity A may retrieve the instance and call setValue(3); 80later activity B may retrieve the instance and call getValue() to 81retrieve 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> 85fields and/or methods. You can access these static fields from any other 86class in your application. To share an object, the activity which creates your object sets a 87static field to point to this object and any other activity that wants to use 88this 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 92keys. When an activity wants to pass an object to another activity, it 93simply puts the object in the map and sends the key (which is a unique 94Long based on a counter or time stamp) to the recipient activity via 95intent extras. The recipient activity retrieves the object using this 96key.</p> 97 98<h3>Persistent Objects</h3> 99 100<p>Even while an application appears to continue running, the system 101may choose to kill its process and restart it later. If you have data 102that you need to persist from one activity invocation to the next, you 103need to represent that data as state that gets saved by an activity when 104it is informed that it might go away.</p> 105 106<p>For sharing complex persistent user-defined objects, the 107following 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 117process can be killed, then place that data in persistent storage like 118Application Preferences, SQLite DB, Files or ContentProviders. Please refer to 119the <a href="{@docRoot}guide/topics/data/data-storage.html">Data Storage</a> 120for 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 128it?</h2> 129 130<p>The general mechanism to start a new activity if its not running— 131or to bring the activity stack to the front if is already running in the 132background— is the to use the NEW_TASK_LAUNCH flag in the startActivity() 133call.</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 140Service to pass a message back to the Activity?</h2> 141 142<p>See the {@link android.app.Service} documentation's for examples of 143how clients can interact with a service. You can take advantage of the 144fact that your components run in the same process to greatly simplify 145service interaction from the generic remote case, as shown by the "Local 146Service 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> 154document.</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 165is broadcast by the system. Similarly when a package is removed, an 166intent with PACKAGE_REMOVED action is broadcast. To receive these 167intents, you should write something like this: 168<pre> 169 <receiver android:name ="com.android.samples.app.PackageReceiver"> 170 <intent-filter> 171 <action android:name="android.intent.action.PACKAGE_ADDED"/> 172 <action android:name="android.intent.action.PACKAGE_REMOVED"/> 173 174 <data android:scheme="package" /> 175 </intent-filter> 176 </receiver> 177 </pre> 178 <br> 179Here PackageReceiver is a BroadcastReceiver class.Its onReceive() 180method is invoked, every time an application package is installed or 181removed. 182 183</p> 184 185 186 187