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>The android.app.Application class</h4> 72 <p>The android.app.Application is a base class for those who need to 73maintain global application state. It can be accessed via 74getApplication() from any Activity or Service. It has a couple of 75life-cycle methods and will be instantiated by Android automatically if 76your 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> 80fields and/or methods. You can access these static fields from any other 81class in your application. To share an object, the activity which creates your object sets a 82static field to point to this object and any other activity that wants to use 83this 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 87keys. When an activity wants to pass an object to another activity, it 88simply puts the object in the map and sends the key (which is a unique 89Long based on a counter or time stamp) to the recipient activity via 90intent extras. The recipient activity retrieves the object using this 91key.</p> 92 93 <h4>A Singleton class</h4> 94 <p>There are advantages to using a static Singleton, such as you can 95refer to them without casting getApplication() to an 96application-specific class, or going to the trouble of hanging an 97interface on all your Application subclasses so that your various 98modules can refer to that interface instead. </p> 99<p>But, the life cycle of a static is not well under your control; so 100to abide by the life-cycle model, the application class should initiate and 101tear down these static objects in the onCreate() and onTerminate() methods 102of 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 108may choose to kill its process and restart it later. If you have data 109that you need to persist from one activity invocation to the next, you 110need to represent that data as state that gets saved by an activity when 111it is informed that it might go away.</p> 112 113<p>For sharing complex persistent user-defined objects, the 114following 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 124process can be killed, then place that data in persistent storage like 125Application Preferences, SQLite DB, Files or ContentProviders. Please refer to 126the <a href="{@docRoot}guide/topics/data/data-storage.html">Data Storage</a> 127for 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 135it?</h2> 136 137<p>The general mechanism to start a new activity if its not running— 138or to bring the activity stack to the front if is already running in the 139background— is the to use the NEW_TASK_LAUNCH flag in the startActivity() 140call.</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 147Service 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 150clients to callback into the clients. The 151{@link android.os.RemoteCallbackList RemoteCallbackList} class provides methods to 152register and unregister clients with the service, and send and receive 153messages.</p> 154 155<p>The sample code for remote service callbacks is given in <a 156href="{@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> 165document.</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 176is broadcast by the system. Similarly when a package is removed, an 177intent with PACKAGE_REMOVED action is broadcast. To receive these 178intents, you should write something like this: 179<pre> 180 <receiver android:name ="com.android.samples.app.PackageReceiver"> 181 <intent-filter> 182 <action android:name="android.intent.action.PACKAGE_ADDED"/> 183 <action android:name="android.intent.action.PACKAGE_REMOVED"/> 184 185 <data android:scheme="package" /> 186 </intent-filter> 187 </receiver> 188 </pre> 189 <br> 190Here PackageReceiver is a BroadcastReceiver class.Its onReceive() 191method is invoked, every time an application package is installed or 192removed. 193 194</p> 195 196 197 198