1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
page.title=Android Application Framework FAQ
@jd:body
<ul>
<li><a href="#1">Do all the Activities and Services of an
application run in a single process?</a></li>
<li><a href="#2">Do all Activities run in the main thread of
an application process?</a></li>
<li><a href="#3">How do I pass complicated data structures
from one Activity/Service to another?</a></li>
<li><a href="#4">How can I check if an Activity is already
running before starting it?</a></li>
<li><a href="#5">If an Activity starts a remote service,is
there any way for the Service to pass a message back to the Activity?</a></li>
<li><a href="#6">How to avoid getting the Application not
responding dialog?</a></li>
<li><a href="#7">How does an application know if a package is
added or removed?</a></li>
</ul>
<!-- ------------------------------------------------------------------ -->
<a name="1" id="1"></a>
<h2>Do all the Activities and Services of an application run in a
single process?</h2>
<p>All Activities and Services in an application run in a single
process by default. The <a href="{@docRoot}reference/android/R.styleable.html#AndroidManifestActivity_process">android:process</a> attribute can be used to
explicitly place a component (Activity/Service) in another process.</p>
<!-- ------------------------------------------------------------------ -->
<a name="2" id="2"></a>
<h2>Do all Activities run in the main thread of an application
process?</h2>
<p>By default, all of the application code in a single process runs
in the main UI thread. This is the same thread
that also handles UI events. The only exception is the code that handles
IPC calls coming in from other processes. The system maintains a
separate pool of transaction threads in each process to dispatch all
incoming IPC calls. The developer should create separate threads for any
long-running code, to avoid blocking the main UI thread.</p>
<!-- ------------------------------------------------------------------ -->
<a name="3" id="3"></a>
<h2>How do I pass data between Activities/Services within a single
application?</h2>
<p>It depends on the type of data that you want to share:</p>
<h3>Primitive Data Types</h3>
<p>To share primitive data between Activities/Services in an
application, use Intent.putExtras(). For passing primitive data that
needs to persist use the
<a href="{@docRoot}devel/data/preferences.html">Application
Preferences</a>.</p>
<h3>Non-Persistent Objects</h3>
<p>For sharing complex non-persistent user-defined objects for short
duration, the following approaches are recommended:
</p>
<h4>The android.app.Application class</h4>
<p>The android.app.Application is a base class for those who need to
maintain global application state. It can be accessed via
getApplication() from any Activity or Service. It has a couple of
life-cycle methods and will be instantiated by Android automatically if
your register it in AndroidManifest.xml.</p>
<h4>A public static field/method</h4>
<p>An alternate way to make data accessible across Activities/Services is to use <em>public static</em>
fields and/or methods. You can access these static fields from any other
class in your application. To share an object, the activity which creates your object sets a
static field to point to this object and any other activity that wants to use
this object just accesses this static field.</p>
<h4>A HashMap of WeakReferences to Objects</h4>
<p>You can also use a HashMap of WeakReferences to Objects with Long
keys. When an activity wants to pass an object to another activity, it
simply puts the object in the map and sends the key (which is a unique
Long based on a counter or time stamp) to the recipient activity via
intent extras. The recipient activity retrieves the object using this
key.</p>
<h4>A Singleton class</h4>
<p>There are advantages to using a static Singleton, such as you can
refer to them without casting getApplication() to an
application-specific class, or going to the trouble of hanging an
interface on all your Application subclasses so that your various
modules can refer to that interface instead. </p>
<p>But, the life cycle of a static is not well under your control; so
to abide by the life-cycle model, the application class should initiate and
tear down these static objects in the onCreate() and onTerminate() methods
of the Application Class</p>
</p>
<h3>Persistent Objects</h3>
<p>Even while an application appears to continue running, the system
may choose to kill its process and restart it later. If you have data
that you need to persist from one activity invocation to the next, you
need to represent that data as state that gets saved by an activity when
it is informed that it might go away.</p>
<p>For sharing complex persistent user-defined objects, the
following approaches are recommended:
<ul>
<li>Application Preferences</li>
<li>Files</li>
<li>contentProviders</li>
<li>SQLite DB</li>
</ul>
</p>
<p>If the shared data needs to be retained across points where the
application process can be killed, then place that data in persistent
storage like Application Preferences, SQLite DB, Files or ContentProviders. Please refer
to the <a href="{@docRoot}devel/data.html">Storing, Retrieving and Exposing Data</a> for further details on how to use these components.</p>
<!-- ------------------------------------------------------------------ -->
<a name="4" id="4"></a>
<h2>How can I check if an Activity is already running before starting
it?</h2>
<p>The general mechanism to start a new activity if its not running—
or to bring the activity stack to the front if is already running in the
background— is the to use the NEW_TASK_LAUNCH flag in the startActivity()
call.</p>
<!-- ------------------------------------------------------------------ -->
<a name="5" id="5"></a>
<h2>If an Activity starts a remote service, is there any way for the
Service to pass a message back to the Activity?</h2>
<p>The remote service can define a callback interface and register
it with the clients to callback into the clients. The
<a href="{@docRoot}reference/android/os/RemoteCallbackList.html">RemoteCallbackList</a> provides methods to register and
unregister clients with the service, and send and receive messages.</p>
<p>The sample code for remote service callbacks is given in
<a href="{@docRoot}samples/ApiDemos/src/com/example/android/apis/app/RemoteService.html">ApiDemos/RemoteService</a></p>
<!-- ------------------------------------------------------------------ -->
<a name="6" id="6"></a>
<h2>How to avoid getting the Application not responding dialog?</h2>
<p>Please check the <a href="{@docRoot}toolbox/responsiveness.html">Application
Responsiveness</a> section to design your application for
better responsiveness:</p>
<!-- ------------------------------------------------------------------ -->
<a name="7" id="7"></a>
<h2>How does an application know if a package is added or removed?
</h2>
<p>Whenever a package is added, an intent with PACKAGE_ADDED action
is broadcast by the system. Similarly when a package is removed, an
intent with PACKAGE_REMOVED action is broadcast. To receive these
intents, you should write something like this:
<pre>
<receiver android:name ="com.android.samples.app.PackageReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<action android:name="android.intent.action.PACKAGE_REMOVED"/>
<data android:scheme="package" />
</intent-filter>
</receiver>
</pre>
<br>
Here PackageReceiver is a BroadcastReceiver class.Its onReceive()
method is invoked, every time an application package is installed or
removed.
</p>
<!-- ------------------------------------------------------------------ -->
|