I have an Activity that starts and binds to a Service . Then I have another Activity launched from the first. Upon returning to the first Activity from the second, I need to call the Service method (it saves some data).
When viewing each Activity my Activity lifecycle methods seem to adequately cope with screen orientation changes, provided that I return to the same screen orientation before exiting the Activity .
The problem arises when I return to the first operation with a different orientation, when I left it. If this happens, I will lose the link to my Service and, therefore, will onActivityResult() NullPointerException in onActivityResult() . Therefore, if I launched the second Activity in portrait mode, switch to landscape view when viewing the second Activity and return to the first Activity in landscape mode, this will work.
What can i skip? I do not want to use the manifest file to indicate that I will handle configuration changes - it puts me as a somewhat ugly hack that does not affect the main problem. If I donβt miss anything else ...
Here are excerpts from my life cycles from the first action:
@Override protected void onStart() { super.onStart(); // start and bind to service startService(smsIntent); connection = new SMServiceConnection(); bindService(smsIntent, connection, Context.BIND_AUTO_CREATE); } @Override protected void onRestart() { super.onRestart(); } @Override protected void onResume() { super.onResume(); } @Override protected void onPause() { super.onPause(); sms.save(); // autosave } @Override protected void onStop() { super.onStop(); unbindService(connection); // stopService(smsIntent); //doesn't appear to have any effect } @Override protected void onDestroy() { super.onDestroy(); }
EDIT: Here are excerpts from my SMServiceConnection class, which is a private inner class in my Activity, which extends from the custom ServiceConnection class.
@Override public void onServiceConnected(ComponentName name, IBinder service) { super.onServiceConnected(name, service); msg("Service connected"); sms = getSMService(); if (sms != null) { String s = sms.getStuff();
My ServiceConnection super complex looks something like this:
public class MyServiceConnection implements ServiceConnection { private boolean serviceAvailable = false; private SMService sms; public void onServiceConnected(ComponentName name, IBinder service) { serviceAvailable = true; LocalBinder b = (LocalBinder) service; sms = b.getService(); } public void onServiceDisconnected(ComponentName name) { serviceAvailable = false; } public boolean isServiceAvailable() { return serviceAvailable; } public SMService getSMService() { return sms; } }