On Mon, Aug 8, 2011 at 8:11 AM, Mark Murphy <[email protected]> wrote:
> Getting binding right with an activity given configuration changes is
> a PITA. If you are using fragments, you may be able to bind from the
> fragment and use setRetainInstance(true) to minimize the pain, though
> I haven't tried this combination yet.
>
Yes you can use a retained fragment for this.
Doing this with an Activity isn't *too* hard:
ServiceConnection mConnection;
public void onCreate(Bundle ss) {
super.onCreate(ss);
ServiceConnection conn =
(ServiceConnection)getLastNonConfigurationInstance();
if (conn == null) {
ServiceConnection conn = new MyConnection();
if (!getApplicationContext().bindService(someIntent, conn)) {
conn = null;
}
}
mConnection = conn;
}
public Object onRetainNonConfigurationInstance() {
ServiceConnection conn = mConnection;
mConnection = null;
return conn;
}
public void onDestroy() {
super.onDestroy();
if (mConnection != null) {
getApplicationContext().unbindService(mConnection);
mConnection = null;
}
}
That is the general pattern for retaining state across activity instances
(if not using the new fragment APIs).
--
Dianne Hackborn
Android framework engineer
[email protected]
Note: please don't send private questions to me, as I don't have time to
provide private support, and so won't reply to such e-mails. All such
questions should be posted on public forums, where I and others can see and
answer them.
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en