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